(以下方法来自Stackoverflow)
禁止RecyclerView滑动可以通过重写LinearLayoutManager中的方法实现。
原始LinearLayoutManager:
public class CustomGridLayoutManager extends LinearLayoutManager { private boolean isScrollEnabled = true; public CustomGridLayoutManager(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(); } }
LinearLayoutManager中的isScrollEnabled标签返回的的值可以控制RecyclerView是否滑动。
为RecyclerView实例化LinearLayoutManager可以通过重写canScrollVertivally(垂直布局)或者canScrollHorizontally(水平布局)方法实现禁止滑动
linearLayoutManager = new LinearLayoutManager(context) {
@Override
public boolean canScrollVertically() {
return false;
}
};