Recycler向左滑动并向右滑动执行不同的操作

时间:2017-03-18 13:58:39

标签: android android-recyclerview swipe

我有以下回收站视图代码,允许用户向右滑动(从视图中删除卡片并从SQLite数据库中删除数据),并允许用户移动标签以重新排列。有用。我很高兴。

ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN,  ItemTouchHelper.RIGHT) {
        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
            //awesome code when user grabs recycler card to reorder   
        } 

        @Override
        public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
            super.clearView(recyclerView, viewHolder);
            //awesome code to run when user drops card and completes reorder
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
            //awesome code when swiping right to remove recycler card and delete SQLite data
        }
    };
    ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
    itemTouchHelper.attachToRecyclerView(recyclerView);

现在,我想添加滑动LEFT并执行与滑动RIGHT不同的功能。到目前为止我找到的所有解决方案都需要重写我的代码。有关将SWIPE LEFT添加到现有代码的任何建议吗?

3 个答案:

答案 0 :(得分:3)

onSwiped方法上,添加 -

if (direction == ItemTouchHelper.RIGHT) {
//whatever code you want the swipe to perform
}

然后另一个左刷卡 -

if (direction == ItemTouchHelper.LEFT) {
//whatever code you want the swipe to perform
}

答案 1 :(得分:1)

利用int lastRowNum = sh.getLastRowNum(); for (int i = 1; i < (lastRowNum+1); i++) { Row row = sh.getRow(i); System.out.println(i); // Null pointer exception throws for below if condition if (row.getCell(0).toString().length() == 0) { actions.operations(objectRepository, row.getCell(1).toString(), row.getCell(2).toString(), row.getCell(3).toString(), row.getCell(4).toString()); } else { System.out.println("Test Case: " + row.getCell(0).toString() + " Started"); } } 的{​​{1}}参数。请参阅documentation。您可以根据您是向右还是向左滑动进行调整。

答案 2 :(得分:0)

对我来说,方向返回START / END而不是LEFT / RIGHT,所以这就是我必须要做的。

    if(direction == ItemTouchHelper.START) {
        // DO Action for Left

    } else if(direction == ItemTouchHelper.END) {
        // DO Action for Right

    }