【问题标题】:RecyclerView wrap_content not working on API 23 and aboveRecyclerView wrap_content 不适用于 API 23 及更高版本
【发布时间】:2019-10-06 22:28:37
【问题描述】:

我在 LinearLayout 中有一个 RecyclerView 布局,该布局也在自定义 NestedScrollView 中。在 api 21 和 22 中,布局看起来应该显示 RecyclerView 的所有元素,但在 api 23 及更高版本中,仅显示一两个项目,而屏幕的其余部分为空白。我知道 RecyclerView 的重点是不使用 wrap_content,但我的理解是你可以。

我注意到,当 RecyclerView 上方的视图可见时,recyclerview 上的 wrap_content 可以正常工作,但在特定情况下,我遇到了问题,这些视图都以编程方式设置为消失,所以它似乎有事可做接着就,随即。所以我不知道该怎么做,因为这些观点应该已经消失了。这是我无法解决的 android sdk 错误吗?

<CustomNestedScrollView
                    android:id="@+id/editProfileScroll"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scrollbars="vertical">

                    <LinearLayout
                        android:id="@+id/editProfileMainContainer"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="@dimen/material_baseline_grid_10x"
                        android:orientation="vertical">

                        <!-- More code: TextViews and TextViews inside LinearLayouts -->

                        <androidx.recyclerview.widget.RecyclerView
                            android:id="@+id/editProfileFieldsRV"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                    </LinearLayout>

                </CustomNestedScrollView>

【问题讨论】:

  • 尝试使用RelativeLayout 而不是LinearLayout
  • @HB。那行得通!如果您提交实际答案,我会将其标记为正确。
  • 下面回答:)

标签: android android-recyclerview android-linearlayout android-nestedscrollview android-wrap-content


【解决方案1】:

尝试使用RelativeLayout 而不是LinearLayout


使用LinearLayout 你必须设置

android:orientation="vertical"
android:weight_sum="3"

在每个元素中你必须添加android:layout_weight="1"

通过执行上述操作,它将在垂直轴上均匀分布 3 个项目。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-22
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-09
    • 2015-02-07
    相关资源
    最近更新 更多