我正在进行拖放操作,现在正像启动器应用程序一样实现删除功能。 我在选择回收商项目时可以看到删除图像,但无法通过拖放删除图像来删除回收商品。 我正在使用Itemtouchhelperadapter和集合来选择和拖动回收物品。
public RecyclerListAdapter(Context context, OnStartDragListener dragStartListener) {
mDragStartListener = dragStartListener;
mItems.addAll(Arrays.asList(context.getResources().getStringArray(R.array.dummy_items)));
}
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
v = parent;
trashView = (ImageView)v.getRootView().findViewById(R.id.main_trash);
trashView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Log.v("inside","ontouch");
return false;
}
});
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main, parent, false);
ItemViewHolder itemViewHolder = new ItemViewHolder(view);
return itemViewHolder;
}
@Override
public void onBindViewHolder(final ItemViewHolder holder, int position) {
holder.textView.setText(mItems.get(position));
// Start a drag whenever the handle view it touched
holder.handleView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
mDragStartListener.onStartDrag(holder);
}
return false;
}
});
}
@Override
public void onItemDismiss(int position) {
mItems.remove(position);
notifyItemRemoved(position);
}
@Override
public boolean onItemMove(int fromPosition, int toPosition) {
Collections.swap(mItems, fromPosition, toPosition);
notifyItemMoved(fromPosition, toPosition);
return true;
}
@Override
public int getItemCount() {
return mItems.size();
}
public static class ItemViewHolder extends RecyclerView.ViewHolder implements
ItemTouchHelperViewHolder {
public final TextView textView;
public final ImageView handleView;
ImageView trashView;
public ItemViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.text);
handleView = (ImageView) itemView.findViewById(R.id.handle);
}
@Override
public void onItemSelected() {
trashView = (ImageView)v.getRootView().findViewById(R.id.main_trash);
trashView.setVisibility(View.VISIBLE);
itemView.setBackgroundColor(Color.LTGRAY);
TextView tv = (TextView)itemView.findViewById(R.id.text);
selectedText = tv.getText().toString();
}
@Override
public void onItemClear() {
((ImageView)v.getRootView().findViewById(R.id.main_trash)).setVisibility(View.GONE);
itemView.setBackgroundColor(0);
}
}
答案 0 :(得分:0)
由于您的代码充满了反复试验,因此您应该将其从任何不必要的内容中清除。
首先,您需要CoordinatorLayout
包装现有布局,为其指定Id
,然后在Activity/Fragment
中对其进行初始化,同时实施View.OnDragListener
并实施其所需方法。稍后会详细介绍。 :)
其次,通话设置onDragListener
:
coordinatorLayout.setOnDragListener(this);
将implements View.OnLongClickListener
添加到Adapter
,按照以下方式实施所需的方法:
@Override
public boolean onLongClick(View v) {
v.startDrag(ClipData.newPlainText("label", v.getTag().toString()), new View.DragShadowBuilder(v), null, 0);
return true;
}
在tag
中设置onLongClickListener
和onBindViewHolder
:
@Override
public void onBindViewHolder(CustomHolder holder, int position) {
holder.itemView.setTag(position);
holder.itemView.setOnLongClickListener(this);
}
以下是onDrag
方法,其中发生了所有魔法:
@Override
public boolean onDrag(View v, DragEvent event) {
/*
* You need to know the `Rect` (Rectangles) aka, the shape of your dragged `View` and your `View`, where you want to drop it.
*/
Rect draggedImageRect = new Rect();
Rect trashRect = new Rect();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_ENTERED:
return true;
case DragEvent.ACTION_DRAG_STARTED:
return true;
case DragEvent.ACTION_DRAG_LOCATION:
return true;
case DragEvent.ACTION_DRAG_EXITED:
return true;
case DragEvent.ACTION_DRAG_ENDED:
return true;
case DragEvent.ACTION_DROP:
/*
* The magic happens here. The user dragged your `View` to the `trashImage` and drops it:
*/
float oldX = v.getX();
float oldY = v.getY();
v.setX(event.getX());
v.setY(event.getY());
v.getHitRect(draggedImageRect);
trashImage.getHitRect(trashRect); //change trashImage with your variable
/*
* More magic happens here. We need to make sure, that only the drop to the trash will delete the dragged item.
*/
if (Rect.intersects(draggedImageRect, trashRect)) {
try {
int pos = Integer.parseInt(event.getClipData().getItemAt(0).getText().toString());
yourList.remove(pos);
yourAdapter.notifyItemRemoved(pos);
} catch (Exception e) {
e.printStackTrace();
}
v.setX(oldX);
v.setY(oldY);
return true;
default:
return false;
}
}