Android 6.0 解决Recyclerview 在 Scrollview 中不能高度自适应问题

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);
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值