Android: how can I insert a RecyclerView inside CardView?

坚强是说给别人听的谎言 提交于 2019-11-30 02:01:08

I faced the exact same problem. You have to specify the layout height of the RecyclerView inside the CardView. Change it from wrap_content to a specific value in dp. Nested RecyclerView doesnt wrap the content in its height if the parent scrolling is VERTICAL and similarly doesnt wrap the content in its width when the parent scrolling is HORIZONTAL.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_analysis"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_marginBottom="@dimen/activity_vertical_margin" 
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:padding="5dp"
card_view:cardCornerRadius="5dp" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <android.support.v7.widget.RecyclerView
        android:id="@+id/item_mode"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:paddingTop="5dp"
        android:paddingLeft="@dimen/activity_horizontal_margin" >

    </android.support.v7.widget.RecyclerView>
</LinearLayout>
</android.support.v7.widget.CardView>

As of support library 23.2, the LayoutManager API brings auto-measurement which allows a RecyclerView to size itself based on the size of its contents. This means that using WRAP_CONTENT for a dimension of the RecyclerView, are now possible. You’ll find all built in LayoutManagers now support auto-measurement.

Source

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!