如何禁用 RecyclerView 滚动? [英] How to disable RecyclerView scrolling?

查看:41
本文介绍了如何禁用 RecyclerView 滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在 RecyclerView 中禁用滚动.我尝试调用 rv.setEnabled(false) 但我仍然可以滚动.

I cannot disable scrolling in the RecyclerView. I tried calling rv.setEnabled(false) but I can still scroll.

如何禁用滚动?

推荐答案

为此,您应该覆盖 recycleViewlayoutManager.这样它只会禁用滚动,不会禁用任何其他功能.您仍然可以处理点击或任何其他触摸事件.例如:-

You should override the layoutManager of your recycleView for this. This way it will only disable scrolling, none of the other functionalities. You will still be able to handle click or any other touch events. For example:-

原文:

public class CustomGridLayoutManager extends LinearLayoutManager {
    private boolean isScrollEnabled = true;

    public CustomGridLayoutManager(Context context) {
        super(context);
    }

    public void setScrollEnabled(boolean flag) {
        this.isScrollEnabled = flag;
    }

    @Override
    public boolean canScrollVertically() {
        //Similarly you can customize "canScrollHorizontally()" for managing horizontal scroll
        return isScrollEnabled && super.canScrollVertically();
    }
}

这里使用isScrollEnabled"标记您可以暂时启用/禁用回收视图的滚动功能.

Here using "isScrollEnabled" flag you can enable/disable scrolling functionality of your recycle-view temporarily.

还有:

简单地覆盖您现有的实现以禁用滚动并允许点击.

Simple override your existing implementation to disable scrolling and allow clicking.

linearLayoutManager = new LinearLayoutManager(context) {
    @Override
    public boolean canScrollVertically() {
        return false;
    }
};

在 Kotlin 中:

object : LinearLayoutManager(this){ override fun canScrollVertically(): Boolean { return false } }

这篇关于如何禁用 RecyclerView 滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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