我想将回收者视图项目图像拖放到我的图像视图(在主布局中)。我怎样才能做到这一点?
我在下面的代码中使用longItemClickRecyclerViewItem
images_recycler.addOnItemTouchListener(
new RecyclerItemClickListener(getActivity(), images_recycler ,new RecyclerItemClickListener.OnItemClickListener() {
@Override public void onItemClick(View view, int position) {
}
@Override
public void onItemLongClick(View view, int position) {
}
})
);
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener
{
public static interface OnItemClickListener
{
public void onItemClick(View view, int position);
void onItemLongClick(View view, int position);
}
private OnItemClickListener mListener;
private GestureDetector mGestureDetector;
public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener)
{
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener()
{
@Override
public boolean onSingleTapUp(MotionEvent e)
{
return true;
}
@Override
public void onLongPress(MotionEvent e)
{
View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
if(childView != null && mListener != null)
{
mListener.onItemLongClick(childView, recyclerView.getChildPosition(childView));
}
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e)
{
View childView = view.findChildViewUnder(e.getX(), e.getY());
if(childView != null && mListener != null && mGestureDetector.onTouchEvent(e))
{
mListener.onItemClick(childView, view.getChildPosition(childView));
}
return false;
}
@Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent){}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
请帮我一些示例或代码 感谢。
答案 0 :(得分:1)
在这里查看我的答案Drag View and Drop it on RecyclerView item Android
但是你必须在这里做一些改变。在longClickListener
方法中为您的回收者视图iteView
设置onCreateViewHolder
。在那里你开始拖动。将onDragListener
设置为您要删除的ImageView
。
编辑
好的,我在这里添加一些代码。在您的回收器适配器onCreateViewHolder
中添加以下行:
@Override
public MemberHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.yourlayout, parent, false);
view.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
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);
}
return true;
}
});
return memberHolder;
}
在图像视图中要删除项目的位置添加以下行:
imageView.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;
}
});
编辑2
好的,这是一个解释。将侦听器分配给视图时,您告诉您要侦听拖动事件。当一个拖动事件开始时,监听器的onDrag会被动作DragEvent.ACTION_DRAG_STARTED
触发。在这种情况下,在要删除项目的视图侦听器上,返回true表示此视图要捕获拖动项目。当视图被删除时,您正在通过操作onDrag
再次触发DragEvent.ACTION_DROP
。在那里你必须实现逻辑,当你删除项目时你想要做什么。如果要更改imageview的图像源或替换imageview,则必须在DragEvent.ACTION_DROP
情况下实现替换逻辑。拖动事件和监听器只会告诉您正在拖动以及丢弃的位置。所以任何其他逻辑必须由你完成。在我的情况下,我通知我的recyclerview适配器某些项目已经更改。