Android 禁止RecyclerView 上下滚动

为什么是禁止RecyclerView 上下滚动呢?先看效果图。

Android 禁止RecyclerView 上下滚动_第1张图片

发现这八个模块竟然可以轻微的上下滚动。那,怎么解决呢?其实对于这个业务需求,只需要禁止RecyclerView 滑动就行了,代码如下:

recyViewAdapter = new RecyViewAdapter(getActivity(), moduleList);

    recyclerView = (RecyclerView)findViewById(R.id.recycle_view);

    MyGridLayoutManager gridLayoutManager=new MyGridLayoutManager(this,4);

    gridLayoutManager.setScrollEnabled(false);

    recyclerView.setLayoutManager(gridLayoutManager);

    recyclerView.setAdapter(recyViewAdapter );

下面就是自己定义的 MyGridLayoutManager :

public class MyGridLayoutManager extends GridLayoutManager {
private boolean isScrollEnabled = true;
public MyGridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
}

public MyGridLayoutManager(Context context, int spanCount) {
    super(context, spanCount);
}

public MyGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) {
    super(context, spanCount, orientation, reverseLayout);
}

public void setScrollEnabled(boolean flag) {
    this.isScrollEnabled = flag;
}

@Override
public boolean canScrollVertically() {

    return isScrollEnabled && super.canScrollVertically();
}

}

ok,打完收工。

你可能感兴趣的:(Android)