RecyclerView wrap_content不起作用.TextView后不显示 [英] RecyclerView wrap_content not working.TextView after it not showing

查看:97
本文介绍了RecyclerView wrap_content不起作用.TextView后不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了无法解决的问题.我有一个包含RecyclerViewCardView片段.此RecyclerView具有layout_height="wrap_content",但我可以看到它不起作用.我在这里阅读了一些解决方案,说在支持库的23.2版中,此问题已修复,但不起作用就我而言.另外,我要在CardView下方的TextView,但是它没有显示出来.如果我给CardView一个固定的高度,那就很好了.

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.

在这里,RecyclerView限制是蓝色矩形,而它们应该类似于红色矩形,这就是我要存档的内容.

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

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"/>

推荐答案

好,终于使它生效了.我不想回答自己的问题,但也许可以帮助其他人.

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

我是在责怪RecyclerView,但有罪是SwipeRefreshLayout.实际上,作为一个新手,我是有罪的.问题是我将SwipeRefreshLayout放在CardView内.对于我所看到的,SwipeRefreshLayout试图获取所有可用空间(我认为这是一种逻辑行为),所以这就是为什么我的CardView这样做的原因.

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.

因此解决方案很简单:在顶部,将SwipeRefreshLayout放在其中,将LinearLayout放入其中,然后将CardViewRecyclerViewTextView放入其中.

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 :)

这篇关于RecyclerView wrap_content不起作用.TextView后不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆