Android-Recycler视图设置项无法滑动 [英] Android - Recycler view set item not swipeable

查看:1066
本文介绍了Android-Recycler视图设置项无法滑动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用RecyclerViewItemTouchHelper.SimpleCallback => onSwiped方法.回收者视图中的某些项目正在进行中,我想在其上禁用滑动.

I am using a RecyclerView and ItemTouchHelper.SimpleCallback => onSwiped method. Some of the items at the recycler view are in progress and i want to disable swipe on them.

我该怎么办?

推荐答案

您可以覆盖

You can override getSwipeDirs() in your ItemTouchHelper.SimpleCallback. Return 0 when you don't want an item to be swipe-able.

此方法的第二个参数是要滑动项目的ViewHolder-您可以在ViewHolder上添加一个简单的标志,以指示该项目是否可滑动.

The second parameter to this method is the ViewHolder for the item being swiped- you can add a simple flag to your ViewHolder to indicate whether or not it is swipe-able.

例如:

static class SwipeViewHolder extends RecyclerView.ViewHolder {
    public boolean isSwipeable;

    public SwipeViewHolder(View itemView) {
        super(itemView);
    }
}

和回调:

static class DragAndSwipeCallback extends ItemTouchHelper.SimpleCallback {

    public DragAndSwipeCallback(int dragDirs, int swipeDirs) {
        super(dragDirs, swipeDirs);
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        // Perform drag
        return false;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        // Remove item
    }

    @Override
    public int getSwipeDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        if (viewHolder instanceof SwipeViewHolder
                && ((SwipeViewHolder) viewHolder).isSwipeable) {
            return super.getSwipeDirs(recyclerView, viewHolder);
        } else {
            return 0;
        }
    }
}

这篇关于Android-Recycler视图设置项无法滑动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆