将RecyclerView中的项目移动到另一个位置

时间:2017-01-10 10:38:18

标签: android firebase android-recyclerview firebase-realtime-database firebaseui

我在RecyclerView中有一个项目列表,它从Firebase数据库中填充。我想通过长按和拖动来实现RecyclerView中的项目移动。 (类似于谷歌的笔记应用程序“保持”)

我已经看过其他一些类似的问题,但我找不到任何适合我需要的解决方案。

遵循一些代码示例。

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);
}

1 个答案:

答案 0 :(得分:0)

由于您使用的是FirebaseUI的FirebaseRecyclerAdapter,因此数据将按照您从数据库中检索的顺序显示。

这意味着要重新排序数据,您需要创建一个新查询为您的数据添加一个属性,您可以使用该属性来设置订单。执行后者的常用方法是在记录中添加index属性。然后,当用户将记录拖到不同的位置时,您“只需”更新所有受影响记录的索引。