【发布时间】: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