如何在没有拖动页眉和页脚的情况下在recyclerview中创建拖放?

时间:2016-11-16 09:29:21

标签: android drag-and-drop android-recyclerview

我使用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());
    }

}

1 个答案:

答案 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);
    }
}

然后,只有你的项目应该拖放,而你的页眉和页脚不能手动移动。