首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >wrap_content中的多个SwipeRefreshLayout和RecyclerView

wrap_content中的多个SwipeRefreshLayout和RecyclerView
EN

Stack Overflow用户
提问于 2018-08-27 23:18:11
回答 1查看 455关注 0票数 0

我有一个带有2个SwipeRefreshLayoutRecyclerView的布局,它只有在layout_height固定的情况下才能工作(例如: 200dp)。如果将其设置为wrap_content,则这些项不可见。

我已经尝试了很多解决方案,但仍然不起作用。我的布局:

代码语言:javascript
复制
<?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

我的构建:

代码语言:javascript
复制
compileSdkVersion 27
minSdkVersion 14
targetSdkVersion 27
implementation 'com.android.support:recyclerview-v7:27.1.1'

我的代码:

代码语言:javascript
复制
recyclerView1.setHasFixedSize(true);
recyclerView1.setNestedScrollingEnabled(false);

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

我也尝试过使用MyLinearLayoutManager

代码语言:javascript
复制
public class MyLinearLayoutManager extends android.support.v7.widget.LinearLayoutManager

但它不起作用。

有解决方案吗?

非常感谢

EN

回答 1

Stack Overflow用户

发布于 2018-08-28 03:07:08

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

说明:

如果您不希望回收器视图的大小(宽度或高度)发生变化,则应将setHasFixedSize设置为true。但是,在您的示例中,您希望它的高度为wrap_content。因此,您需要删除此行。

发生的情况如下:

回收器视图最初没有项目,它会获得一个高度为.

  • Next,的项目已添加。但是,它已被设置为固定大小,因此将不再调整大小。

  • ,因此我们看不到新的行。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52042373

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档