要禁止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);