我正在开发一个Android应用程序,其屏幕包含以下内容:
答案 0 :(得分:9)
首先在您的回收器适配器的onCreateViewHolder
中的膨胀视图中添加一个draglistener。
view.setOnDragListener(new OnDragListener() {
@Override
public boolean onDrag(View view, DragEvent dragEvent) {
switch (dragEvent.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
// drag has started, return true to tell that you're listening to the drag
return true;
case DragEvent.ACTION_DROP:
// the dragged item was dropped into this view
Category a = items.get(getAdapterPosition());
a.setText("dropped");
notifyItemChanged(getAdapterPosition());
return true;
case DragEvent.ACTION_DRAG_ENDED:
// the drag has ended
return false;
}
return false;
}
});
在ACTION_DROP
情况下,您可以更改模型并调用notifyItemChanged()
,也可以直接修改视图(不会处理重新绑定的情况)。同样在onCreateViewHolder
中为longClickListener
添加View
,并在onLongClick
开始拖动:
ClipData.Item item = new ClipData.Item((CharSequence) view.getTag());
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData dragData = new ClipData(view.getTag().toString(),
mimeTypes, item);
view.setVisibility(View.GONE);
DragShadowBuilder myShadow = new DragShadowBuilder(view);
if (VERSION.SDK_INT >= VERSION_CODES.N) {
view.startDragAndDrop(dragData, myShadow, null, 0);
} else {
view.startDrag(dragData, myShadow, null, 0);
}
有关拖放的详细信息,请查看android developers site