学堂 学堂 学堂公众号手机端

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

lewis 2年前 (2023-09-20) 阅读数 5 #技术

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

publicclassCustomLayoutManagerextendsLinearLayoutManager{ privatebooleanisScrollEnabled=true; publicCustomLayoutManager(Contextcontext){ super(context); } publicvoidsetScrollEnabled(booleanflag){ this.isScrollEnabled=flag; } @Override publicbooleancanScrollHorizontally(){ returnisScrollEnabled&&super.canScrollHorizontally(); } @Override publicbooleancanScrollVertically(){ returnisScrollEnabled&&super.canScrollVertically(); } }

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

CustomLayoutManagerlayoutManager=newCustomLayoutManager(getContext()); layoutManager.setScrollEnabled(false); recyclerView.setLayoutManager(layoutManager);

版权声明

本文仅代表作者观点,不代表博信信息网立场。

热门