【问题标题】:RecyclerView does not expand its height when an item is added on adapter在适配器上添加项目时,RecyclerView 不会扩展其高度
【发布时间】:2017-09-21 12:25:06
【问题描述】:

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

此布局是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>

目前的状态是这样的:

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

【问题讨论】:

  • 你试过在 RecyclerView 上调用“requestLayout”吗?
  • 是的,也没有用..
  • @gkanellis 你能解决这个问题吗?

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:
    1. 将linearlayout的layout_height改为match_parrent。
    2. 将 recyclerview 的 layout_height 更改为 match_parrent。
    3. 将您的 recyclerviews 项目的高度更改为 wrap_content。

    试试这个

    【讨论】:

    • 将 linearlayout 的 layout_height 设置为 match_parrent 不起作用。如果我将 recyclerview 的 layout_height 设置为 match_parrent,那么由于 LinearLayout 的行为,“显示更多”按钮将被隐藏。
    • 所以将线性布局改为相对布局。
    【解决方案3】:

    替换这个

     <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

    【讨论】:

    • 没用。仍然 recyclerView 只能滚动是添加第一个项目的区域。
    • 还将 Recycler 视图高度更改为 match_parent
    【解决方案4】:

    检查这个修改后的布局。 为 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>
    

    【讨论】:

      猜你喜欢
      • 2011-08-25
      • 2015-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多