1

I've stumped in a problem I can't solve. I've got a fragment with a CardView containing a RecyclerView. This RecyclerView has layout_height="wrap_content" but i can see this is not working. I've read some solutions here saying that in version 23.2 of the support library this is fixed, but it doesn't work in my case. Also, i want a TextView right below the CardView, but it is not showing it. It's working fine if i give the CardView a fixed height.

I hope someone could help with this.

EDIT Let's see if this image explains better what i mean.

enter image description here

Here,the RecyclerView limits are the blue rectangle, while they should be similar to the red one, that is what i want to archieve.

build.gradle

dependencies{
          compile 'com.android.support:recyclerview-v7:24.+'
}

layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
          >
    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_gravity="top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="2dp"
        android:padding="5dp"
        card_view:cardElevation="4dp">
          <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipeContainer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

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

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

    </android.support.v7.widget.CardView>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:text="test text"
        android:textSize="20sp"/>

Community
  • 1
  • 1
Pikoh
  • 7,582
  • 28
  • 53
  • So you want your layout to contain both recyclerview and a text view? – Eenvincible Jul 14 '16 at 17:22
  • @Eenvincible well,that's the idea. But the main problem is the `ReciclerView` wrap_content not working. The textview problem could be solved with a layout with weigths i believe,but then it won't be right below. – Pikoh Jul 14 '16 at 17:25
  • Have wrap_content for the RecyclerView will definitely hide the text view below when you have enough items to fill up the visible layout; that is why setting a height will make the textview visible. Let me try an answer from what I have – Eenvincible Jul 14 '16 at 17:32
  • Yes,you are right @eenvincible,but it also happens when there's only one element in the recyclerview,so the real problem is the recyclerview getting all the available space, so it's not wrapping the content... – Pikoh Jul 14 '16 at 17:36

2 Answers2

10

Ok, at last I made it work. I don't like to answer my own question, but maybe can help others.

I was blaming RecyclerView,but the guilty was SwipeRefreshLayout. And in fact I as a newbie was the guilty. The problem was that I was putting the SwipeRefreshLayout inside the CardView. For what I can see, SwipeRefreshLayout tries to get all the available space (I think it is a logical behaviour) so that's why my CardView was doing the same.

So the solution is simple: on top, put the SwipeRefreshLayout, inside it a LinearLayout and inside it the CardView with the RecyclerView and the TextView.

Hope this could help other newbies as me :)

Pikoh
  • 7,582
  • 28
  • 53
0

I have an <include @layout/some_layout /> file that looks like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@android:color/white"
  app:layout_behavior="@string/appbar_scrolling_view_behavior"
  tools:showIn="@layout/activity_main">

 <!-- Wrap this whole thing in a cardView -->
  <android.support.v7.widget.RecyclerView
     android:id="@+id/recycler"
     android:layout_width="match_parent"
     android:layout_height="wrap_content">
  </android.support.v7.widget.RecyclerView>

  <!-- End of cardview layout -->

  <TextView
     android:layout_width="match_parent"
     android:layout_height="300dp"
     android:text="test text"
     android:layout_below="@id/recycler"
     android:textSize="20sp"/>

</RelativeLayout>

What I am starting to think also is to place both recyclerview and the textview inside your card view. That means making CardView be your Root Layout. Just my thoughts!

Eenvincible
  • 5,641
  • 2
  • 27
  • 46
  • Ok,i'll try it and i'll come back with the results. Thank you – Pikoh Jul 14 '16 at 18:27
  • I've tried and same results. As i told before, the problem is the RecyclerView taking up all the available space – Pikoh Jul 14 '16 at 18:48
  • Okay then I would suggest putting both recyclerview and textview inside one CardView – Eenvincible Jul 14 '16 at 18:50
  • Yes,that would solve the textview problem,but...it doesn't look right. Forget about the TextView, the cardview is extending to fill all available space, and what i want is it to just have the contents height, so it really wraps the items in the recyclerview. Thanks for your ideas anyway :) – Pikoh Jul 14 '16 at 18:55
  • You can see the image in my updated question to better understand what i mean – Pikoh Jul 14 '16 at 19:07