Android开发常用代码Android开发Android技术知识

Recyclerview在scrollview中禁止滑动及在An

2017-03-27  本文已影响1142人  暗尘随码去

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

禁止滑动解决方法

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();
    }
}
MyGridLayoutManager gridLayoutManager = new 
MyGridLayoutManager(getContext(),2);//两列
        gridLayoutManager.setScrollEnabled(false);
        recycleView_single.setLayoutManager(gridLayoutManager);

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

<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>
上一篇下一篇

猜你喜欢

热点阅读