拖动查看并将其放在RecyclerView项目Android上 [英] Drag View and Drop it on RecyclerView item Android
问题描述
我正在开发一个包含以下内容的屏幕的Android应用程序:
I am developing an android application that has a screen contains the following:
- 包含如下图所示类别的回收器视图
- Recycler view that contains categories as the below figure
- Separate view at the botton and the user should able to drag it on a RecyclerView item, and after drop the user will show changing at the RecyclerView item data (for example items count at the category)
我需要一些帮助,如何实现这个过程
将View拖入Recycler项目,下图解释了我想做,但不知道该怎么做
I need some help about how to implement this process to drag View into Recycler item, the following figure explains exactly what I want to do but have no idea how to do it
任何帮助是非常感谢
推荐答案
首先向 onCreateViewHolder
您的回收商适配器。
Start by adding a draglistener to your inflated view in onCreateViewHolder
of your recycler adapter.
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
到您的查看
,以及在 onLongClick
开始拖动:
In the ACTION_DROP
case you can either change the model and call notifyItemChanged()
, or modify directly the view (that won't handle the rebinding case). Also in onCreateViewHolder
add a longClickListener
to your View
, and in onLongClick
start the drag:
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);
HeptagonDragShadowBuilder myShadow = new HeptagonDragShadowBuilder(Heptagon.this, 1.1f);
if (VERSION.SDK_INT >= VERSION_CODES.N) {
view.startDragAndDrop(dragData, myShadow, null, 0);
} else {
view.startDrag(dragData, myShadow, null, 0);
}
有关拖放的更多信息,请查看 android开发者网站
For more information about drag and drop check out the android developers site
这篇关于拖动查看并将其放在RecyclerView项目Android上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!