【问题标题】:RecyclerView wrap_content not working.TextView after it not showingRecyclerView wrap_content 不工作。TextView 后不显示
【发布时间】:2016-07-14 17:14:08
【问题描述】:

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

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

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

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

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

【问题讨论】:

  • 所以你想让你的布局同时包含recyclerview和一个文本视图?
  • @Eenvincible 很好,就是这样。但主要问题是ReciclerView wrap_content 不起作用。 textview 问题可以通过我相信重量的布局来解决,但它不会在下面。
  • 有 wrap_content 为 RecyclerView 当你有足够的项目来填满可见布局时,肯定会隐藏下面的文本视图;这就是为什么设置高度会使文本视图可见。让我尝试从我所拥有的答案
  • 是的,你是对的@eenvincible,但是当recyclerview中只有一个元素时也会发生这种情况,所以真正的问题是recyclerview获取了所有可用空间,所以它没有包装内容.. .

标签: android android-recyclerview


【解决方案1】:

好的,我终于成功了。我不喜欢回答自己的问题,但也许可以帮助别人。

我在责备RecyclerView,但有罪的是SwipeRefreshLayout。事实上,我作为一个新手是有罪的。问题是我将SwipeRefreshLayout 放在CardView 中。就我所见,SwipeRefreshLayout 试图获取所有可用空间(我认为这是一种合乎逻辑的行为),所以我的CardView 也在这样做。

所以解决方案很简单:在顶部放置SwipeRefreshLayout,在其中放置一个LinearLayout,在其中放置CardView,以及RecyclerView 和TextView。

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

【讨论】:

    【解决方案2】:

    我有一个如下所示的&lt;include @layout/some_layout /&gt; 文件:

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

    我开始想到的也是将 recyclerview 和 textview 放在您的卡片视图中。这意味着使 CardView 成为您的根布局。只是我的想法!

    【讨论】:

    • 好的,我会试试,我会回来的结果。谢谢
    • 我试过了,结果一样。正如我之前所说,问题是 RecyclerView 占用了所有可用空间
    • 好的,那么我建议将 recyclerview 和 textview 放在一个 CardView 中
    • 是的,这可以解决 textview 问题,但是……它看起来不正确。忘记TextView,cardview正在扩展以填充所有可用空间,而我想要的是它只有内容高度,所以它真的将项目包装在recyclerview中。无论如何感谢您的想法:)
    • 您可以在我更新的问题中查看图片以更好地理解我的意思
    猜你喜欢
    • 2020-09-19
    • 1970-01-01
    • 1970-01-01
    • 2015-02-13
    • 2023-03-11
    • 1970-01-01
    • 2015-05-06
    • 2016-06-15
    • 1970-01-01
    相关资源
    最近更新 更多