2

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

我浏览过的所有示例都解释了 Vertical Recyclerview 的 ItemTouchHelper 或网格中的项目。

从以下链接跟踪样本:
https ://medium.com/@ipaulpro/drag-and-swipe-with-recyclerview-b9456d2b1aaf
https://medium.com/@xabaras/recyclerview-swiping-with-style-151e21b1af07

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

4

2 回答 2

2

您可以使用这个简单的代码来实现向下滑动删除。

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)
于 2018-10-25T09:14:11.523 回答
1

您只需要更改方法“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;
}
于 2018-10-25T10:50:45.570 回答