【问题标题】:How to make the RecyclerView forbid Scroll如何让 RecyclerView 禁止滚动
【发布时间】:2017-09-22 06:32:38
【问题描述】:

在某些情况下,我们需要让 RecyclerView 不能滚动,所以这道题要发挥你的长处。

【问题讨论】:

    标签: android scroll android-recyclerview


    【解决方案1】:

    我有一些解决这个问题的办法,希望对你有帮助。详情见下文:

    1. void setLayoutFrozen (boolean freeze);
      如果我们只想使用滚动,我强烈推荐,因为这个方案简单方便; if you want to know deeply

    2. 当我们使用LayoutManager(LinearManager或GridLayoutManager)时,我们可以使用这个。

    `

    LinearLayoutManager linearLayoutManager = new 
    
    LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false){
    
            @Override
            public boolean canScrollVertically() {
                return false;
            }
    
            @Override
            public boolean canScrollHorizontally() {
                return super.canScrollHorizontally();
            }
        };`
    

    GridLayoutManager 是一样的。

    1. 从上面的第二项,我们可以扩展对应的LayoutManager。

    `

    public class MyGridLayoutManager extends GridLayoutManager {
    
    private boolean isScrollEnabled = true;
    
    public MyGridLayoutManager(Context context, int spanCount) {
        super(context, spanCount);
    }
    public MyGridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }
    
    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();
    }
    } 
    

    ` 4、我们可以处理RecyclerView TouchEvent来实现效果。

    从上面,我相信你可以做自己。祝你好运...

    【讨论】:

    • 顺便说一句,我完全同意这句话:问和回答你自己的问题也很好,只要你假装你在危险中! — 以问题的形式表达出来。
    猜你喜欢
    • 1970-01-01
    • 2015-08-12
    • 2015-09-23
    • 2021-08-13
    • 1970-01-01
    • 2016-07-17
    • 1970-01-01
    相关资源
    最近更新 更多