【问题标题】:Horizontal RecyclerView follows first item height with wrap_content水平 RecyclerView 使用 wrap_content 跟随第一项高度
【发布时间】:2026-01-18 04:30:01
【问题描述】:

我有一个水平布局的RecyclerView。各个适配器项目的高度可能取决于项目的内容。

所以理论上它可以如下所示:

我现在遇到的问题是wrap_content似乎只关心第一个问题的高度,因为我得到的结果是这样的:

如您所见,第 4 项被切断了。但是,如果我将最高的项目放在首位,它会非常有效。

并摆脱明显的解决方案;我不知道物品的高度。我只能在测试期间这样做。

--

adapter_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:animateLayoutChanges="true"
    android:padding="@dimen/padding_view_small">

    <ImageView
        android:id="@+id/image"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="center_horizontal"
        app:srcCompat="@drawable/flamme"/>

    <TextView
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        android:layout_marginTop="@dimen/padding_view_small"
        android:text="@string/placeholder"
        android:gravity="center_horizontal"/>

</LinearLayout>

父.xml

<LinearLayout
    android:id="@+id/symbol_list_parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@drawable/container_content"
    android:padding="@dimen/padding_view_normal">

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

</LinearLayout>

Activity.java

...
final ProductPictogramAdapter symbolAdapter = new ProductPictogramAdapter();
final View symbolListParent = findViewById(R.id.symbol_list_parent);
RecyclerView symbolList = findViewById(R.id.symbol_list);
symbolList.setLayoutManager(new LinearLayoutManager(ProductDetailActivity.this, LinearLayoutManager.HORIZONTAL, false));
symbolList.setAdapter(symbolAdapter);
symbolList.setHasFixedSize(true);

【问题讨论】:

  • 使用 StaggeredGridLayoutManager *.com/questions/46190573/…
  • 我决定使用 Google 的 FlexBox 中的 FlexboxLayoutManager 来均匀地显示所有项目。在玩过它之后,我认为一次显示所有项目而不是滚动它们会更好。
  • 尝试删除 setHasFixedSize 属性,我也面临同样的问题,然后我删除了这个属性。

标签: android android-recyclerview


【解决方案1】:

由于这个问题引起了很大的关注,我只在 cmets 中发布了我的个人解决方案,所以我也会提交一个答案。

--

我决定使用Google's Flexbox layout 库来实现我想要的。它非常可定制,我会向尝试解决类似问题的人推荐它。

【讨论】:

    【解决方案2】:

    试试这个。无论如何,你的问题解决了吗?

    mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            final int newHeight = recyclerView.getMeasuredHeight();
            if (0 != newHeight && minHeight < newHeight) {
            // keep track the height and prevent recycler view optimizing by resizing
                minHeight = newHeight;
                recyclerView.setMinimumHeight(minHeight);
            }
        }
    });
    

    【讨论】:

      【解决方案3】:

      我遇到了同样的错误,我用下面的代码解决了;

      binding.recyclerView.setHasFixedSize(false);
      

      【讨论】:

      【解决方案4】:

      将您的图像视图高度更改为 wrap_content。我不确定,这样试试。

      【讨论】: