在notifyDataSetChanged()之后,RecyclerView项目无法处理拖动事件

时间:2018-03-15 08:37:37

标签: android android-recyclerview drag-and-drop

我有简单的RecyclerView(屏幕上没有其他内容)和简单的项目(具有固定高度的简单视图)。

RecyclerView调用时,长按项目触发的本机拖动事件。

每个项目都有调用的dragListener,在示例中我可以在拖动阴影输入或退出项目期间更改视图背景。

但我的情况比较复杂。在拖动过程中,我需要更新我的项目(在此示例中,我通过在notifyDataSetChanged()期间调用方法DragEvent.ACTION_DRAG_LOCATION)来触发它。

问题是在拖动过程中项目发生变化后 - 它们无法处理任何拖动事件。

我的活动代码:

public class MyActivity extends AppCompatActivity {

private RecyclerView recyclerView;

private View.OnLongClickListener onLongClickListener = new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        recyclerView.startDrag(
                new ClipData(new ClipDescription("drag", new String[]{ClipDescription.MIMETYPE_TEXT_PLAIN}), new ClipData.Item("drag")),
                new View.DragShadowBuilder(v),
                null,
                0
        );
        return true;
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = findViewById(R.id.recycler_view);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(new MyListAdapter(onLongClickListener));
}
}

我的适配器的代码,其中存在拖动事件的处理:

public class MyListAdapter extends RecyclerView.Adapter<MyListAdapter.Holder>{

private View.OnLongClickListener onLongClickListener;

public MyListAdapter(View.OnLongClickListener longClickListener) {
    onLongClickListener = longClickListener;
}

@Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
    final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
    final Holder holder = new Holder(view);

    view.setOnDragListener(new View.OnDragListener() {
        @Override
        public boolean onDrag(View v, DragEvent event) {
            switch (event.getAction()) {
                case DragEvent.ACTION_DRAG_ENTERED:
                    holder.setSelected(true);
                    return true;
                case DragEvent.ACTION_DRAG_EXITED:
                case DragEvent.ACTION_DROP:
                    holder.setSelected(false);
                    return true;
                case DragEvent.ACTION_DRAG_LOCATION:
                    notifyDataSetChanged();
                    return true;
                default:
                    return true;
            }
        }
    });

    return holder;
}

@Override
public void onBindViewHolder(Holder holder, int position) {
    holder.setContent(position % 2 == 0 ? R.color.colorPrimary : R.color.colorAccent);
}

@Override
public int getItemCount() {
    return 100;
}

class Holder extends RecyclerView.ViewHolder {

    private View view;
    private int colorRes;

    public Holder(View itemView) {
        super(itemView);
        view = itemView;
        itemView.setOnLongClickListener(onLongClickListener);
    }

    public void setContent(int colorRes) {
        this.colorRes = colorRes;
        view.setBackgroundResource(colorRes);
    }

    public void setSelected(boolean selected) {
        view.setBackgroundResource(selected ? R.color.green : colorRes);
    }
}
}

0 个答案:

没有答案