项目中会遇到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>