在适配器上添加项目时,RecyclerView不会扩展其高度

时间:2017-09-21 12:25:06

标签: android android-recyclerview android-viewpager android-tablayout android-collapsingtoolbarlayout

我有一个包含RecyclerView的布局和一个用于显示更多项目的布局。当通过点击“显示更多”按钮收到一个或多个项目时,将通知适配器并将项目添加到RecyclerView上。 问题是,在添加项目后,RecyclerView的高度未展开。

此布局是ViewPagerCollapsingToolbarLayout中包含的片段的一部分。

片段布局的结构:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#f5f5f5">

<com.github.rahatarmanahmed.cpv.CircularProgressView
    android:id="@+id/progress_view"
    android:layout_width="45dp"
    android:layout_height="45dp"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="40dp"
    app:cpv_animAutostart="true"
    app:cpv_indeterminate="true"
    app:cpv_thickness="4dp"/>

<LinearLayout
    android:id="@+id/list_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/reviews_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <FrameLayout
        android:id="@+id/show_more_container"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#eee">

        <TextView
            android:id="@+id/show_more_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Show More"
            android:textSize="15sp"
            android:textColor="@color/colorPrimary"/>

        <com.github.rahatarmanahmed.cpv.CircularProgressView
            android:id="@+id/show_more_progress_view"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:visibility="invisible"
            app:cpv_animAutostart="true"
            app:cpv_indeterminate="true"
            app:cpv_thickness="3dp"/>

    </FrameLayout>

</LinearLayout>

</FrameLayout>

目前的状态是这样的:

State

  • 图像的黑色部分是设备的可用高度。

4 个答案:

答案 0 :(得分:1)

  1. 将linearlayout的layout_height更改为match_parrent。
  2. 将recyclerview的layout_height更改为match_parrent。
  3. 将您的recyclerviews项目的高度更改为wrap_content。
  4. 试试这个

答案 1 :(得分:0)

替换此

 <LinearLayout
        android:id="@+id/list_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

 <LinearLayout
            android:id="@+id/list_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

同时将Recyclerview高度更改为match_parent

答案 2 :(得分:0)

将您的LinearLayout更改为RelativeLayout,将FrameLayout锚定到底部父级,将RecyclerView更改为FrameLayout,如下所示:

<RelativeLayout android:id="@+id/list_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/reviews_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/show_more_container" />

    <FrameLayout
        android:id="@+id/show_more_container"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:background="#eee">

        <TextView
            android:id="@+id/show_more_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Show More"
            android:textSize="15sp"
            android:textColor="@color/colorPrimary"/>

        <com.github.rahatarmanahmed.cpv.CircularProgressView
            android:id="@+id/show_more_progress_view"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:visibility="invisible" />

    </FrameLayout>

</RelativeLayout>

RecyclerView身高改为match_parent

答案 3 :(得分:0)

检查此修改后的布局。 为recyclerview和按钮增加重量会使它固定在特定的位置。希望它适合你。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#f5f5f5">

<com.github.rahatarmanahmed.cpv.CircularProgressView
    android:id="@+id/progress_view"
    android:layout_width="45dp"
    android:layout_height="45dp"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="40dp"
    app:cpv_animAutostart="true"
    app:cpv_indeterminate="true"
    app:cpv_thickness="4dp"/>

<LinearLayout
    android:id="@+id/list_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/reviews_list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.9"/>

    <FrameLayout
        android:id="@+id/show_more_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.1"
        android:background="#eee">

        <TextView
            android:id="@+id/show_more_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Show More"
            android:textSize="15sp"
            android:textColor="@color/colorPrimary"/>

        <com.github.rahatarmanahmed.cpv.CircularProgressView
            android:id="@+id/show_more_progress_view"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:visibility="invisible"
            app:cpv_animAutostart="true"
            app:cpv_indeterminate="true"
            app:cpv_thickness="3dp"/>

    </FrameLayout>

</LinearLayout>