0

I've a layout with 2 SwipeRefreshLayout and RecyclerView, it works only if the layout_height is fixed (ex: 200dp). If it is set to wrap_content, the items are not visible.

I've tried a lot of solutions but it continues to not work. My layout:

<?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>

In this case, only first RecyclerView is showed

My build:

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

My code:

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

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

I've also tried with MyLinearLayoutManager

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

but it does not work.

Is there a solution?

Thanks a lot

Giulio
  • 221
  • 2
  • 15
  • Does it help solving your problem when you use NestedScrollView instead of scrollview? – jle Aug 27 '18 at 15:22
  • @Joe S not work – Giulio Aug 27 '18 at 15:36
  • Okay. Is the list filled or is it empty? – jle Aug 27 '18 at 15:37
  • @Joe S is filled because if I change wrap_content with fixed height the items is showed – Giulio Aug 27 '18 at 15:40
  • @Joe S I don't have understand the question. The problem is in wrap_content the recycleview is not showed. With single SwipeRefreshLayout and RecyclerView the item is showed (height is match_parent), but both are not showed (height=wrap_content), are showed only fixed height – Giulio Aug 27 '18 at 15:48
  • I know, I deleted the comment already. For me it is not entirely clear what you want to achieve. Do you want to have the `swipeRefreshLayout` and the `RecyclerView` both as `wrap_content`, or what to you desire to get? – jle Aug 27 '18 at 15:51
  • both wrap_content, 200dp is only for test it, I've modified the layout with desire – Giulio Aug 27 '18 at 15:58
  • @Joe S I've tried to remove swipeRefreshLayout and it works fine. Do you know why with swipeRefreshLayout it not works? – Giulio Aug 27 '18 at 16:07
  • I don't know if that might work, but set the `SwipeRefreshLayout` height to `match_parent`. I tried it and for me it doesn't seem to overleap the list. If it doesn't work, switch again to `NestedScrollView` – jle Aug 27 '18 at 16:18
  • @Joe S not work – Giulio Aug 27 '18 at 16:32
  • Okay, then I don't see it like that. I can only offer you to have a look at the whole project, so I can test it right – jle Aug 27 '18 at 17:34

1 Answers1

0

In short, you should remove the setHasFixedSize(true) lines.

Explanation:

setHasFixedSize should be set to true if you do not expect the recyclerview to change in size (either width or height). However, in your case you want it to have a wrap_content height. So you need to remove this line.

What happens is as follows:

  • The recyclerview initially has no items, it gets a height of 0.
  • Next, items are added. However, it has been set to a fixed size, so it will not resize anymore.
  • So we don't see the new rows visible.

This is also the reason why you see items when setting the height to a certain value.

Alex
  • 1,650
  • 16
  • 15