我创建了一个简单的RecyclerView,我正在使用下面的Swipe Listener:
//swipe items
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
//do nothing, we only care about swiping
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
if(swipeDir == ItemTouchHelper.RIGHT){
Toast.makeText(getContext(), "Swiped right", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getContext(), "Swiped left", Toast.LENGTH_SHORT).show();
}
}
}).attachToRecyclerView(recyclerView);
我想只允许向右滑动并防止向左滑动。因此,当用户尝试向左滑动时,我希望项目不会消失。
我该怎么做?
答案 0 :(得分:3)
如果您只想启用一些滑动/拖动动作,则需要覆盖@Override public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder)
的方法ItemTouchHelper.Callback
(超级类ItemTouchHelper.SimpleCallback
)
例如,如果您只想启用右侧滑动:
@Override public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
int dragFlags = 0;
int swipeFlags = ItemTouchHelper.RIGHT;
return makeMovementFlags(dragFlags, swipeFlags);
}