[英]Android CardView Drag and Drop ItemTouchHelper
I tried to look all over google and SO but couldn't find a solution to my problem. 我试图遍及Google和SO,但找不到解决我问题的方法。
Basically, I've got a RecyclerView utilizing CardViews, and I want to allow Drag&Drop on those cardviews. 基本上,我有一个使用CardViews的RecyclerView,我想在这些Cardview上允许拖放。 Swiping works fine, but in the ItemTouchHelper, Drag and drop doesn't work.
滑动工作正常,但在ItemTouchHelper中,拖放无效。 I'm not sure why, I've specified the correct movement directions.
我不确定为什么,我指定了正确的运动方向。
I can swipe left and right for the swipeDirs, but Moving up and down doesn't work for the dragDirs. 对于swipeDirs,我可以左右滑动,但是对于dragDirs,不能上下移动。 I'm not sure if its an issue with the emulator itself not recognizing "drags" because if I attach UP and DOWN to the swipeDirs, I can swipe in every direction as a test.
我不确定模拟器本身是否无法识别“拖动”问题,因为如果在swipeDirs上附加了UP和DOWN,则可以在各个方向上滑动以进行测试。
I'm not getting any visuals of Drag and Drop working 我没有任何拖放的视觉效果
ItemTouchHelper.Callback scb = new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT){
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target){
//call back to adapter to swap positions, the Error is not with this line of code (at least not yet)
return true;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction){
}
};
ItemTouchHelper ith = new ItemTouchHelper(scb);
ith.attachToRecyclerView(recyclerView);
Any help is appreciated, Thanks 任何帮助表示赞赏,谢谢
Figured it out, you need to Override onBindViewHolder in your Adapter 弄清楚了,您需要在适配器中覆盖onBindViewHolder
@Override
public void onBindViewHolder(Adapter.ViewHolder holder, int position){
final Adapter.ViewHolder xholder = holder;
holder.card.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View view, MotionEvent event){
if(MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN)
listener.onStartDrag(xholder);
return false;
}
});
}
And then in your RecyclerList containing the ItemTouchHelper 然后在您的RecyclerList中包含ItemTouchHelper
@Override
public void onStartDrag(ViewHolder holder){
itemTouchHelper.startDrag(holder);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.