水平 RecyclerView 跟随第一个项目高度和 wrap_content

Ezz*_*zzy 21 android android-recyclerview

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

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

在此处输入图片说明

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

在此处输入图片说明

如您所见,第 4 项被切断。但是,如果我把最高的项目放在第一位,它就可以完美地工作。

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

——

适配器项.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>
Run Code Online (Sandbox Code Playgroud)

父文件

<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>
Run Code Online (Sandbox Code Playgroud)

活动.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);
Run Code Online (Sandbox Code Playgroud)

Ezz*_*zzy 1

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

--

我决定使用Google 的 Flexbox 布局库来实现我想要的。它是非常可定制的,我会推荐它给那些试图解决类似问题的人。