Android 6.0 解决Recyclerview 在 Scrollview 中不能高度自适应问题
在项目中遇到解决Recyclerview 在 Scrollview 中不能高度自适应问题:android6.0以下机器是可以的,但是6.0就不能自适应,经网上查询应该是一个bug。
在网上查询资料终于找到解决方法
http://stackoverflow.com/questions/27083091/recyclerview-inside-scrollview-is-not-working
其他网站重写LayoutManager的方法试了都不行。
最终解决办法很简单,如下:
在 recyclerview 外面再嵌套一层 RelativeLayout即可。
代码 如下:
xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:divider="@null"
android:fadingEdge="none"
android:listSelector="@android:color/transparent"
android:padding="@dimen/margin_horizontal_mid"
android:scrollbarStyle="outsideOverlay"
app:layoutManager="android.support.v7.widget.GridLayoutManager"
app:spanCount="4"
tools:listitem="@layout/adapter_repair_image_list_item" />
</RelativeLayout>
activity:
private void initWidget() {
selImageList = new ArrayList<>();
adapter = new ImagePickerAdapter(this, selImageList, maxImgCount);
adapter.setOnItemClickListener(this);
mRecyclerViewImage.setLayoutManager(new GridLayoutManager(this, 4));
mRecyclerViewImage.setHasFixedSize(true);
mRecyclerViewImage.setAdapter(adapter);
}
以上方法亲测可用。
现在建议使用
NestedScrollView+RecyclerView 嵌套了 这样更丝滑
//布局xml文件的RecyclerView中设置
android:nestedScrollingEnabled="false"
//或者代码设置
recyclerView.setNestedScrollingEnabled(false);