Recyclerview在scrollview中禁止滑动及在Android 6.0 不能全部显示的解决。

项目中会遇到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>
    原文作者:暗尘随码去
    原文地址: https://www.jianshu.com/p/49bf92baa98b
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞