【问题标题】:Multiple SwipeRefreshLayout and RecyclerView in wrap_contentwrap_content 中的多个 SwipeRefreshLayout 和 RecyclerView
【发布时间】:2018-08-27 15:18:10
【问题描述】:

我有一个包含 2 个 SwipeRefreshLayout 和 RecyclerView 的布局,它仅在 layout_height 固定时才有效(例如:200dp)。如果设置为wrap_content,则项目不可见。

我尝试了很多解决方案,但仍然无法正常工作。 我的布局:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        tools:ignore="UselessParent">

        <TextView
            android:id="@+id/feedNews1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:gravity="center"
            android:paddingBottom="10dp"
            android:paddingTop="10dp"
            android:text=""
            android:textColor="#FFFFFF"
            android:textSize="@dimen/text_feed_rss_title_size" />

        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipeRefreshLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp">

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

        <TextView
            android:id="@+id/feedNews2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:gravity="center"
            android:paddingBottom="10dp"
            android:paddingTop="10dp"
            android:text=""
            android:textColor="#FFFFFF"
            android:textSize="@dimen/text_feed_rss_title_size" />


        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipeRefreshLayout2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp" >

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

        </android.support.v4.widget.SwipeRefreshLayout>

        <LinearLayout
            android:id="@+id/adsContainerNews"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="2dp"
            android:gravity="center"
            android:orientation="vertical"
            android:visibility="gone">

            <com.google.android.gms.ads.AdView
                android:id="@+id/adViewNews"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                ads:adSize="BANNER"
                ads:adUnitId="@string/banner_ad_unit_id" />
        </LinearLayout>

    </LinearLayout>

</ScrollView>

在这种情况下,只显示第一个RecyclerView

我的构建:

compileSdkVersion 27
minSdkVersion 14
targetSdkVersion 27
implementation 'com.android.support:recyclerview-v7:27.1.1'

我的代码:

recyclerView1.setHasFixedSize(true);
recyclerView1.setNestedScrollingEnabled(false);

recyclerView2.setHasFixedSize(true);
recyclerView2.setNestedScrollingEnabled(false);

我也试过MyLinearLayoutManager

public class MyLinearLayoutManager extends android.support.v7.widget.LinearLayoutManager

但它不起作用。

有解决办法吗?

非常感谢

【问题讨论】:

  • 使用 NestedScrollView 代替 scrollview 是否有助于解决您的问题?
  • @Joe S 不工作
  • 好的。列表是满的还是空的?
  • @Joe S 已填充,因为如果我将 wrap_content 更改为固定高度,则会显示项目
  • @Joe S 我不明白这个问题。问题在于 wrap_content 中没有显示回收视图。使用单个 SwipeRefreshLayout 和 RecyclerView 显示项目(高度为 match_parent),但两者均不显示(height=wrap_content),仅显示固定高度

标签: android android-recyclerview swiperefreshlayout linearlayoutmanager


【解决方案1】:

简而言之,您应该删除 setHasFixedSize(true) 行。

说明:

如果您不期望 recyclerview 的大小(宽度或高度)发生变化,则

setHasFixedSize 应设置为 true。但是,在您的情况下,您希望它具有 wrap_content 高度。所以你需要删除这一行。

发生的情况如下:

  • recyclerview 最初没有项目,它的高度为 0。
  • 接下来,添加项目。但是,它已设置为固定大小,因此不会再调整大小。
  • 所以我们看不到新的行。

这也是为什么在将高度设置为某个值时会看到项目的原因。

【讨论】:

  • 我已经删除了 setHasFixedSize(true) 行,但是项目没有显示
猜你喜欢
  • 2023-03-11
  • 2014-09-30
  • 1970-01-01
  • 2015-10-31
  • 2015-05-06
  • 2017-01-02
  • 2021-01-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多