RecyclerView wrap_content 不工作。TextView 不显示后

Pik*_*koh 1 android android-recyclerview

我遇到了一个我无法解决的问题。我有一个CardView包含 a的片段RecyclerView。这RecyclerView有,layout_height="wrap_content"但我可以看到这是行不通的。我在这里阅读了一些解决方案,说在支持库的 23.2 版中这是固定的,但在我的情况下不起作用。另外,我想要一个在 的TextView正下方CardView,但它没有显示出来。如果我给CardView一个固定的高度,它工作正常。

我希望有人可以帮助解决这个问题。

编辑 让我们看看这张图片是否能更好地解释我的意思。

在此处输入图片说明

在这里,RecyclerView限制是蓝色矩形,而它们应该类似于红色矩形,这就是我想要归档的。

构建.gradle

dependencies{
          compile 'com.android.support:recyclerview-v7:24.+'
}
Run Code Online (Sandbox Code Playgroud)

布局文件

<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"/>
Run Code Online (Sandbox Code Playgroud)

Pik*_*koh 9

好的,最后我让它工作了。我不喜欢回答我自己的问题,但也许可以帮助别人。

我在责怪RecyclerView,但有罪的是SwipeRefreshLayout。事实上,我作为一个新手是有罪的。问题是我把SwipeRefreshLayout里面的CardView. 就我所见,SwipeRefreshLayout试图获得所有可用空间(我认为这是一种合乎逻辑的行为),所以这就是我CardView这样做的原因。

因此,解决方法很简单:在上面,放SwipeRefreshLayout,里面一个LinearLayout和它里面CardView用的RecyclerViewTextView

希望这可以帮助像我这样的其他新手:)