Reputation: 259
I have a list of items in a RecyclerView which gets populated from a Firebase Database. I'd like to implement movement of items inside the RecyclerView by long press and dragging. (Similar to Google's note app "Keep")
I have already seen some other similar questions but I was not able to find any solution which suits my needs.
Following some code examples.
MainActivity (onCreate)
mRecycler = (RecyclerView) findViewById(R.id.messages_list);
mRecycler.setHasFixedSize(true);
mManager = new LinearLayoutManager(getApplicationContext());
mManager.setReverseLayout(false);
mManager.setStackFromEnd(false);
mRecycler.setLayoutManager(mManager);
findViewById(R.id.fab_new_post).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(RecordActivity.this, NewRecordActivity.class));
}
});
// Set up FirebaseRecyclerAdapter with the Query
mAdapter = new FirebaseRecyclerAdapter<Record, RecordViewHolder>(Record.class, R.layout.item_record, RecordViewHolder.class, mDatabase.child("user-record").child(getUid())) {
@Override
protected void populateViewHolder(final RecordViewHolder viewHolder, final Record model, final int position) {
final DatabaseReference postRef = getRef(position);
// Set click listener for the whole post view
final String postKey = postRef.getKey();
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Launch RecordDetailActivity
Intent intent = new Intent(getApplicationContext(), RecordDetailActivity.class);
//MANDARE TESTO A POSTDETAILACTIVITY
intent.putExtra(RecordDetailActivity.EXTRA_POST_KEY, postKey);
intent.putExtra(RecordDetailActivity.TITLE, model.title);
intent.putExtra(RecordDetailActivity.BODY, model.body);
intent.putExtra(RecordDetailActivity.DATE, model.date);
intent.putExtra(RecordDetailActivity.NOTE, model.note);
intent.putExtra(RecordDetailActivity.COLOR, model.color);
startActivity(intent);
}
});
// Bind Record to ViewHolder, setting OnClickListener for the star button
viewHolder.bindToPost( model);
RecordViewHolder
public class RecordViewHolder extends RecyclerView.ViewHolder {
public TextView titleView;
public TextView bodyView;
public TextView dateView;
public TextView noteView;
public TextView colorView;
public CardView cardView;
public RecordViewHolder(View itemView) {
super(itemView);
titleView = (TextView) itemView.findViewById(R.id.post_title);
bodyView = (TextView) itemView.findViewById(R.id.post_body);
dateView = (TextView) itemView.findViewById(R.id.record_item_date);
noteView = (TextView) itemView.findViewById(R.id.record_item_note);
colorView = (TextView) itemView.findViewById(R.id.record_item_color);
cardView = (CardView) itemView.findViewById(R.id.record_item_card);
}
public void bindToPost(Record record) {
titleView.setText(record.title);
bodyView.setText(record.body);
dateView.setText(record.date);
noteView.setText(record.note);
colorView.setText(record.color);
}
Upvotes: 2
Views: 1618
Reputation: 599856
Since you're using FirebaseUI's FirebaseRecyclerAdapter
, the data will be shown in the order in which you retrieve it from the database.
That means to re-order the data, you will need to either create a new query or add a property to your data that you can use to set the order. A common way to do the latter is to add an index
property to your records. Then when the user drags a record to a different position, you'll "just" have to update the index of all affected records.
Upvotes: 1