Android 禁止RecycleView的滑動

1.問題?

使用RecycleView 時,如果數據量很少只有幾個,需求不需要它上下左右滑動,在xml配置中加上Android:scrollbars=”none”,這只是去掉了滑動bar。

但是RecycleView 上下還是能滑動,且有陰影。

2.解決方案

How to disable RecyclerView scrolling?

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

    public CustomLinearLayoutManager(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();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

在 RecyclerView 設置LinearLayout的時候 繼承上述子類,並設置setScrollEnabled 爲false 即可。

      CustomLinearLayoutManager linearLayoutManager = new CustomLinearLayoutManager(mContext);
        linearLayoutManager.setScrollEnabled(false);
        mDevicesRV.setLayoutManager(linearLayoutManager);
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

3.參考鏈接

https://my.oschina.net/u/1446823/blog/712306

http://stackoverflow.com/questions/30531091/how-to-disable-recyclerview-scrolling

發佈了53 篇原創文章 · 獲贊 17 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章