【问题标题】:RecyclerView with ScrollView wrap_content problemsRecyclerView 与 ScrollView wrap_content 问题
【发布时间】:2016-11-07 13:13:00
【问题描述】:

首先,我知道旧版本的库中存在 Recycler 问题。我当前的 lib 版本是 24.2.1。

我正在尝试制作一个模板,其中包含有关该项目的基本信息,并且在此信息下方是一个 cmets 列表。这个 cmets 列表必须包装它的内容并且必须与它的父级一起滚动,所以它应该感觉像是一个大视图。

我正在尝试使用以下代码来实现,但 RecyclerView 不包装内容,而是在屏幕底部有一小部分。

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="50dp">

    <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
        android:layout_width="match_parent" android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary" app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <include layout="@layout/news_layout_header_repost"/>

            <LinearLayout
                android:orientation="vertical" android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/cardview_inner_margin"
                android:layout_marginEnd="@dimen/cardview_inner_margin"
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools">

                <LinearLayout
                    android:id="@+id/repostTextLayout"
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                </LinearLayout>

            </LinearLayout>

            <Button
                android:id="@+id/loadComments"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

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

        </LinearLayout>

    </ScrollView>

</LinearLayout>

你能帮我么?

更新 终于找到了解决办法。最好使用相对布局。不知道为什么LinearLayout会出现这个问题,但是下面的xml结构起作用了:

<ScrollView>
   <RelativeLayout>
      <LinearLayout/>
      <RecyclerView/>
   </RelativeLayout>
</ScrollView>

【问题讨论】:

  • 你找到答案了吗,我也有同样的问题。

标签: android android-recyclerview android-scrollview


【解决方案1】:

ScrollView 不应该有 wrap_content 高度

【讨论】:

    【解决方案2】:

    根据recyclerview包含的子项数量管理recyclerview的高度,或许可以解决高度问题

    【讨论】:

    • 感谢您的回复。我也认为它是第二种选择。这似乎不是最有效和最干净的方法(当然,它可能会解决问题)
    猜你喜欢
    • 2015-08-24
    • 2017-10-26
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多