禁止RecyclerView上下滚动/滑动解决办法,补充系统8.0禁止RecyclerView上下滚动/滑动失效问题

    根据需求,要求RecyclerView不可以滚动,翻阅了不少资料,还是这个最靠谱,自测可行。

 

 1.自定义RecyclerView管理器,我的demo是grid样式,所以extends GridLayoutManager即可。

 

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();
    }
}

 

     注意:若你的RecyclerView所要展示的是列表样式,那便extends LinearLayoutManager即可,其余代码无变化,代码就不贴了。

 

2.接下来应用自定义的MyGridLayoutManager工具类即可。

 

 @BindView(R.id.rvRoom)
 RecyclerView rvRoom;

 MyGridLayoutManager gridLayoutManager = new MyGridLayoutManager(getActivity(),7);
 gridLayoutManager.setScrollEnabled(false);
 rvRoom.setLayoutManager(gridLayoutManager);

3.补充系统8.0禁止RecyclerView上下滚动/滑动失效问题

在recycleview外面套一层RelativeLayout布局,渲染recycleview的时候正常设置就好。

 <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginLeft="15dp"
      android:layout_marginTop="12dp"
      android:layout_marginRight="15dp"
      android:descendantFocusability="blocksDescendants">

       <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="none" />
</RelativeLayout>
  recyclerView.setLayoutManager(new LinearLayoutManager(this));
  MainAdapter adapter = new MainAdapter(this, model.getData());
  recyclerView.setAdapter(adapter);

4.完成了,欢迎留言讨论。


 

  • 7
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值