用于水平RecyclerView的ItemTouchHelper

时间:2018-10-25 08:46:10

标签: android android-recyclerview itemtouchhelper

我正在尝试为Horizo​​ntal Recyclerview实现ItemTouchHelper。 (将布局管理器设置为LinearLineManager,方向为LinearLayoutManager.HORIZONTAL)。例如,我想在向下滑动时删除一个项目,然后向左或向右拖动以交换项目。

我经过的所有示例都解释了VerticalRecyclerview的ItemTouchHelper或网格中的项目。

以下链接中的示例:
https://medium.com/@ipaulpro/drag-and-swipe-with-recyclerview-b9456d2b1aaf
https://medium.com/@xabaras/recyclerview-swiping-with-style-151e21b1af07

如何在Horizo​​ntal Recyclerview中向下滑动以删除并横向拖动以交换项目?

2 个答案:

答案 0 :(得分:0)

您可以使用此简单代码来实现向下滑动以将其删除。

ItemTouchHelper.SimpleCallback simpleCallback = new ItemTouchHelper.SimpleCallback(0,ItemTouchHelper.DOWN) {
            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,  RecyclerView.ViewHolder target) {
            return false;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
            final int position = viewHolder.getLayoutPosition();
            if (direction == ItemTouchHelper.DOWN) {
                //your code for deleting the item from database or from the list
                adapter.removeNote(position);
                adapter.notifyItemRemoved(position)
            }
        }
    };
    ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleCallback);
    itemTouchHelper.attachToRecyclerView(recyclerView);

对于下面的Kotlin极客,这是相同的代码-

val simpleCallback = object : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.DOWN) {
        override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
            return false
        }

        override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
            if (direction == ItemTouchHelper.DOWN) {
                //your code for deleting the item from database or from the list
                val position = viewHolder.adapterPosition
                noteList.removeAt(position)
                adapter.notifyItemRemoved(position)
            }
        }
    }
    val itemTouchHelper = ItemTouchHelper(simpleCallback)
    itemTouchHelper.attachToRecyclerView(recycler_view)

答案 1 :(得分:0)

您只需要更改方法“ getMovementFlags”。

@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 int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
    final int swipeFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
    final int dragFlags = ItemTouchHelper.START | ItemTouchHelper.END;
    return makeMovementFlags(dragFlags, swipeFlags);
}

要删除项目,请在“ ItemTouchHelperAdapter”中创建一个名为“ onSwiped”的新方法,并实施该方法以删除该项目

@Override
public boolean onSwiped(int itemPosition, int direction) {
    if(direction == SimpleItemTouchHelperCallback.SWIPED_TO_END) {
        list.remove(itemPosition);
        notifyItemRemoved(itemPosition);
    }else{
        notifyItemChanged(itemPosition);
    }
    return true;
}