问题
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.
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"/>
回答1:
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 :)
回答2:
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!
来源:https://stackoverflow.com/questions/38380331/recyclerview-wrap-content-not-working-textview-after-it-not-showing