RecyclerView自适应布局高度

作者: waiwaaa | 来源:发表于2020-08-04 16:02 被阅读0次

需求:LinearLayout中嵌入RecyclerView要显示全部内容,但内容的多少条数为未知的

第一步:包装一层RelativeLayout

      <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <android.support.v7.widget.RecyclerView
                    android:id="@+id/cash_coupon_rv"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingBottom="@dimen/distance_size_4"
                    android:paddingTop="@dimen/distance_size_4"
                    android:visibility="visible" />
       </RelativeLayout>

第二步:Adapter的布局的最外层布局保证 android:layout_height="wrap_content"wrap_content属性

方法源自https://my.oschina.net/ccmagic/blog/1929231实测有效。

另外还有方法为,没有试过

    //在设置此属性即可,只有23以上的库有该方法
     LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setAutoMeasureEnabled(true);

相关文章

网友评论

    本文标题:RecyclerView自适应布局高度

    本文链接:https://www.haomeiwen.com/subject/phxfhktx.html