1. 程式人生 > >Android中Recyclerview使用15----禁止RecycleView的滑動

Android中Recyclerview使用15----禁止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.參考連結