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

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

问题描述

我无法禁止在RecyclerView中滚动.我尝试致电rv.setEnabled(false),但仍可以滚动.

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

如何禁用滚动?

推荐答案

为此,您应该覆盖您的recycleview的layoutmanager.这样,它将仅禁用滚动,而没有其他功能.您仍然可以处理点击或任何其他触摸事件.例如:-

You should override the layoutmanager of your recycleview for this. This way it will only disable scrolling, none of ther 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;
 }
};

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

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