CardView高度不会与嵌套的RecyclerView一起转换

时间:2017-03-14 00:10:59

标签: java android xml

我有一个嵌套的RecyclerView,其父视图是CardView ( gif )。我正在尝试通过滑动删除Paul Burke ( link )删除操作。该动作在内部RecyclerView中很好地转换,但导致外部CardView的高度(wrap_content)跳转。有没有办法使用RecyclerView进行CardView转换?

Inner RecyclerView:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/card_tasks_root"
    android:orientation="vertical"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/card_tasks_title"
        android:padding="8dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:text="section placeholder" />

    <android.support.v7.widget.CardView
        android:layout_marginBottom="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fff"
        android:elevation="2dp">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/card_tasks_rv"
            android:background="#E0E0E0"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

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

内在项目:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:background="#fff"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:orientation="vertical"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="7dp"
        android:paddingBottom="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/item_task_project"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            tools:text="Project name"
            />
        <TextView
            android:id="@+id/item_task_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="3dp"
            android:paddingBottom="5dp"
            android:textSize="18dp"
            android:textColor="#757575"
            tools:text="Task name"/>
    </LinearLayout>

    <View android:id="@+id/item_task_divider" style="@style/Divider"/>
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

将CardView设置为项目布局中的父级,并将其加载到RecyclerView中,如下所示:

你的容器:

    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/card_tasks_root"
    android:orientation="vertical"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/card_tasks_title"
        android:padding="8dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:text="section placeholder" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/card_tasks_rv"
            android:background="#E0E0E0"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

</LinearLayout>

内部项目:

<android.support.v7.widget.CardView
    android:layout_marginBottom="5dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#fff"
    android:elevation="2dp">
<LinearLayout 
    android:orientation="vertical"
    android:background="#fff"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:orientation="vertical"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="7dp"
        android:paddingBottom="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/item_task_project"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            tools:text="Project name"
            />
        <TextView
            android:id="@+id/item_task_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="3dp"
            android:paddingBottom="5dp"
            android:textSize="18dp"
            android:textColor="#757575"
            tools:text="Task name"/>
    </LinearLayout>

    <View android:id="@+id/item_task_divider" style="@style/Divider"/>
</LinearLayout>
</android.support.v7.widget.CardView>