onDrag 调用每个 RecyclerView.ViewHolder
onDrag called for each RecyclerView.ViewHolder
我的 onDrag 方法似乎被 RecyclerView 中存在的每个 RecyclerView.ViewHolder 调用。有谁知道如何防止这种情况并确保只调用一次?
ViewHolder
public class ViewHolder extends RecyclerView.ViewHolder implements OnLongClickListener, OnDragListener {
public ImageView imgProduct;
public TextView lblName;
public ViewHolder(View view) {
super(view);
imgProduct = (ImageView)view.findViewById(R.id.imgShelfProduct);
imgProduct.setOnLongClickListener(this);
imgProduct.setOnDragListener(this);
lblName = (TextView) view.findViewById(R.id.lblShelfText);
}
@Override
public boolean onLongClick(View view) {
ClipData data = ClipData.newPlainText("product", view.toString());
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
return false;
}
@Override
public boolean onDrag(View view, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
Log.d(ShoppingApplication.TAG, "Started");
return true;
case DragEvent.ACTION_DRAG_ENTERED:
Log.d(ShoppingApplication.TAG, "Entered");
return true;
case DragEvent.ACTION_DRAG_EXITED:
Log.d(ShoppingApplication.TAG, "Exited");
return true;
case DragEvent.ACTION_DRAG_ENDED:
Log.d(ShoppingApplication.TAG, "Ended");
return true;
default:
break;
}
return false;
}
}
onCreateViewHolder
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_shelf_product, parent, false);
return new ViewHolder(view);
}
onBindViewHolder
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
Product product = products.get(position);
viewHolder.lblName.setText(product.getName());
String url = app.getImagePath();
Picasso.with(activity).load(url).into(viewHolder.imgProduct);
}
我一定是误解了OnDragListener的概念。它不应该添加到可拖动视图中,而是添加到您希望将它们拖动到的视图中。所以我将它移出 RecyclerView.Adapter class 并移入 Activity class 并添加到我的目标视图中。
现在每个触发器只调用一次 onDrag 方法。
我的 onDrag 方法似乎被 RecyclerView 中存在的每个 RecyclerView.ViewHolder 调用。有谁知道如何防止这种情况并确保只调用一次?
ViewHolder
public class ViewHolder extends RecyclerView.ViewHolder implements OnLongClickListener, OnDragListener {
public ImageView imgProduct;
public TextView lblName;
public ViewHolder(View view) {
super(view);
imgProduct = (ImageView)view.findViewById(R.id.imgShelfProduct);
imgProduct.setOnLongClickListener(this);
imgProduct.setOnDragListener(this);
lblName = (TextView) view.findViewById(R.id.lblShelfText);
}
@Override
public boolean onLongClick(View view) {
ClipData data = ClipData.newPlainText("product", view.toString());
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
return false;
}
@Override
public boolean onDrag(View view, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
Log.d(ShoppingApplication.TAG, "Started");
return true;
case DragEvent.ACTION_DRAG_ENTERED:
Log.d(ShoppingApplication.TAG, "Entered");
return true;
case DragEvent.ACTION_DRAG_EXITED:
Log.d(ShoppingApplication.TAG, "Exited");
return true;
case DragEvent.ACTION_DRAG_ENDED:
Log.d(ShoppingApplication.TAG, "Ended");
return true;
default:
break;
}
return false;
}
}
onCreateViewHolder
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_shelf_product, parent, false);
return new ViewHolder(view);
}
onBindViewHolder
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
Product product = products.get(position);
viewHolder.lblName.setText(product.getName());
String url = app.getImagePath();
Picasso.with(activity).load(url).into(viewHolder.imgProduct);
}
我一定是误解了OnDragListener的概念。它不应该添加到可拖动视图中,而是添加到您希望将它们拖动到的视图中。所以我将它移出 RecyclerView.Adapter class 并移入 Activity class 并添加到我的目标视图中。
现在每个触发器只调用一次 onDrag 方法。