安卓6.0以上ScrollView嵌套RecyclerView条目无法全部显示
一般情况下,应当避免ScrollView嵌套RecyclerView, 但有时候就需进行嵌套, 以前嵌套listView会导致条目显示不完整, 如果嵌套RecyclerView在6.0以下系统, 初了滑动不太流畅外其他功能全部正常, recyclerView.setNestedScrollingEnabled(false) 可以解决滑动不流畅.
但是如果是6.0以上系统, 列表不能全部显示, 然后就找了下解决方法, 是在当前recycleView控件上在加一个父布局RelativeLayout 这样就可以解决, 添加其他父布局好像是不可以. 这是一个单独的layout,include_punch_field.xml;
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/punch_stub_recycle"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
这样解决虽然干脆利落, 但是又多了一层布局的嵌套, 而且还是RelativeLayout, 如果在6.0以下系统, 显然没有必要, 所以我就做了下版本判断, 如果系统版本>=23就使用带有RelativeLayout为父布局的recycleView, 否则的话就不用它.
使用ViewStub, 不使用的时候是不占用布局嵌套层次的, 所以当使用的时候在加载.
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ViewStub
android:id="@+id/punch_field_stub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout="@layout/include_punch_field"//RelativeLayout父布局内容
/>
<android.support.v7.widget.RecyclerView
android:id="@+id/punch_field_recycle"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
加载ViewStub 返回一个recycleView, 如果是>=6.0系统就加载viewStub, 否则的话, 还是用不带有RelativeLayout父布局容器的recycleView, ViewStub 只能加载一次, 多次调用会报空指针.
public RecyclerView getRecycleView() {
if (Build.VERSION.SDK_INT >= 23) {
ViewStub punch_field_stub = (ViewStub) findViewById(R.id.punch_field_stub);
punch_field_stub.inflate();//加载
return (RecyclerView) findViewById(R.id.punch_stub_recycle);
}
return (RecyclerView) findViewById(R.id.punch_field_recycle);
}