我使用ItemTouchHelper.Callback在recylerview中创建了拖放,它正在工作。但我在recylerview中有页眉和页脚我不想拖动页眉和页脚。解决这个问题,这是我的代码
recylerviewActivity
ItemTouchHelper.Callback callback = new SwipeAndDrag(pick_up_mAdapter);
ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
touchHelper.attachToRecyclerView(recyclerView);
SwipeAndDrag.java
public class SwipeAndDrag extends ItemTouchHelper.Callback {
private final ItemTouchHelperAdapter mAdapter;
private RouteInformation points;
public SwipeAndDrag(ItemTouchHelperAdapter adapter) {
mAdapter = adapter;
}
@Override
public boolean isLongPressDragEnabled() {
return false;
}
@Override
public boolean isItemViewSwipeEnabled() {
return true;
}
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
return makeMovementFlags(dragFlags, swipeFlags);
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
RecyclerView.ViewHolder target) {
mAdapter.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
return true;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
mAdapter.onItemDismiss(viewHolder.getAdapterPosition());
}
}
答案 0 :(得分:1)
你应该像这样修改你的getMovementFlags方法(测试它似乎没问题):
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder){
if(viewHolder instanceof yourFooterOrHeaderViewholderClass){
int dragFlags = 0; // then, they can't be dragged
int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
return makeMovementFlags(dragFlags, swipeFlags);
}else{ // if(viewHolder instanceof yourNormalItemViewHolderClass)
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
return makeMovementFlags(dragFlags, swipeFlags);
}
}
然后,只有你的项目应该拖放,而你的页眉和页脚不能手动移动。