【问题标题】:RecycleView set wrong height for itemsRecyclerView 为项目设置了错误的高度
【发布时间】:2016-03-02 12:28:00
【问题描述】:

我为我搜索答案,但没有找到适合我的工作答案。我在RecycleView 中查看我的项目时遇到了奇怪的错误:

我对 RecycleView 使用不同的视图。

我尝试使用此代码:

mLayoutManager.setAutoMeasureEnabled(true); // false doesn't work too

但这对我没有帮助。

UPD 1 我忘了注意这一点,抱歉。当我启动应用程序时,我没有错误(图像上的正常项目)但是当我开始滚动我的 RecycleView 时我遇到了问题)

UPD 2我回到compile 'com.android.support:recyclerview-v7:23.1.1',但它对我没有帮助(所以我用我的RecycleView 添加代码和xml,希望你能帮助我。

XML:

<android.support.v7.widget.RecyclerView
    android:id="@+id/dashboard_recycle_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    android:scrollbars="none" />

和代码:

    mRecyclerView = (RecyclerView) parent.findViewById(R.id.dashboard_recycle_view);
    mLayoutManager = new LinearLayoutManager(getContext());
    mLayoutManager.setAutoMeasureEnabled(false);
    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setHasFixedSize(false);
    mRecyclerView.setAdapter(mRecycleAdapter);

【问题讨论】:

  • 你的 recyclerview 的 layout_height 是多少?
  • @Amit Tiwari 我使用 wrap_content。如果我设置 match_parent 我在滚动之前遇到了这个问题。现在,使用 wrap_content 我开始滚动时遇到了这个问题
  • 在早期版本的支持库中,wrap_content 存在一些问题,但是在 23.2 版本中,他们已经修复了 wrap_content 的回收器视图高度。因此,可能是,此版本中仍然存在错误。尝试将回收站视图的 layout_height 设置为某个静态值,例如 500dp,看看它是否正常工作。
  • @AmitTiwari 我无法设置固定大小,因为我在 RecycleView 中有不同的项目((((
  • 您是否尝试在 mLayoutManager 上调用 setMeasurementCacheEnabled(false)?

标签: android android-recyclerview


【解决方案1】:

为 recyclerview 和 viewholder 布局父级设置wrap_content。 这对我有用。

【讨论】:

  • 使用带有 android:layout_height="wrap_content" 的 RelativeLayout 仍然没有显示我嵌套的 recyclerview 的内容。如果我设置一个高度,项目显示没有问题。
【解决方案2】:

设置 wrap_content 而不是 match_parent (如果您设置为 match_parent ),因为这会导致布局与父级匹配,从而为您提供大空间。

【讨论】:

    【解决方案3】:

    我遇到了同样的问题。如果你只把RecyclerView改成wrap_content,启动页看起来还不错,但是上滑后会出现空白。

    解决方案: 除了将RecyclerViewandroid:layout_height 更改为wrap_content 之外,您还应该转到您的RecyclerView 项目所在的布局XML 文件,并将周围的RelativeLayout 的android:layout_height 属性修改为wrap_content

    请参考this thread

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-13
      • 2020-11-20
      • 2021-02-23
      • 2017-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多