android中recyclerview禁止滑动怎么实现

avatar
作者
筋斗云
阅读量:0

要禁止RecyclerView的滑动,可以通过设置其LayoutManager为一个自定义的LayoutManager,并在该自定义LayoutManager中重写canScrollHorizontally()和canScrollVertically()方法,使其返回false,从而禁止RecyclerView的滑动。以下是一个示例代码:

public class CustomLayoutManager extends LinearLayoutManager {     private boolean isScrollEnabled = true;      public CustomLayoutManager(Context context) {         super(context);     }      public void setScrollEnabled(boolean flag) {         this.isScrollEnabled = flag;     }      @Override     public boolean canScrollHorizontally() {         return isScrollEnabled && super.canScrollHorizontally();     }      @Override     public boolean canScrollVertically() {         return isScrollEnabled && super.canScrollVertically();     } } 

然后在使用RecyclerView的地方,将LayoutManager设置为该自定义LayoutManager,并调用setScrollEnabled(false)方法来禁止滑动:

CustomLayoutManager layoutManager = new CustomLayoutManager(getContext()); layoutManager.setScrollEnabled(false); recyclerView.setLayoutManager(layoutManager); 

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!