美文网首页Android开发常用代码Android开发Android技术知识
Recyclerview在scrollview中禁止滑动及在An

Recyclerview在scrollview中禁止滑动及在An

作者: 暗尘随码去 | 来源:发表于2017-03-27 14:59 被阅读1142次

    项目中会遇到scrollview嵌套Recyclerview的布局,所以需要禁止Recyclerview的滚动,已达到布局效果。

    禁止滑动解决方法

    • 自定义LayoutManager以GridLayoutManager为例,LinearLayoutManager同理。
    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();
        }
    }
    
    • 设置Recycleview的LayoutManager
    MyGridLayoutManager gridLayoutManager = new 
    MyGridLayoutManager(getContext(),2);//两列
            gridLayoutManager.setScrollEnabled(false);
            recycleView_single.setLayoutManager(gridLayoutManager);
    

    滑动解决后发现recycleview在安卓6.0显示不全,高度权重再怎么设置都只显示一部分。

    • 解决方法,在使用到recycleview的布局外用一个RelativeLayout,关键属性android:descendantFocusability="blocksDescendants"
    <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:descendantFocusability="blocksDescendants">
                    <android.support.v7.widget.RecyclerView
                        android:background="@color/white"
                        android:id="@+id/recyclerView_single"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />
                </RelativeLayout>
    

    相关文章

      网友评论

        本文标题:Recyclerview在scrollview中禁止滑动及在An

        本文链接:https://www.haomeiwen.com/subject/wkdrottx.html