安卓6.0以上ScrollView嵌套RecyclerView条目无法全部显示
安卓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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
这样解决虽然干脆利落, 但是又多了一层布局的嵌套, 而且还是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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
加载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);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-60ecaf1f42.css" rel="stylesheet">
<div></div>
<div class="person-messagebox">
<div class="left-message"><a href="https://blog.csdn.net/lmy_1992">
<img src="https://profile.csdnimg.cn/9/E/2/3_lmy_1992" class="avatar_pic" username="lmy_1992">
</a></div>
<div class="middle-message">
<div class="title"><span class="tit "><a href="https://blog.csdn.net/lmy_1992" data-report-click="{"mod":"popu_379","ab":"new"}" target="_blank">我是男子汉</a></span>
<!-- 等级,level -->
<img class="identity-icon" src="https://csdnimg.cn/identity/blog2.png"> </div>
<div class="text"><span>原创文章 10</span><span>获赞 3</span><span>访问量 1万+</span></div>
</div>
<div class="right-message">
<a class="btn btn-sm bt-button personal-watch" data-report-click="{"mod":"popu_379","ab":"new","extend1":"个人信息未关注"}">关注</a>
<a href="https://im.csdn.net/im/main.html?userName=lmy_1992" target="_blank" class="btn btn-sm bt-button personal-letter">私信
</a>
</div>
</div>
</div>
</article>