【发布时间】:2019-10-23 17:46:58
【问题描述】:
我想使用水平滚动回收器。
为此,我使用了LinearLayoutManager (LinearLayoutManager.HORIZONTAL),这是可行的,但是为项目获得了不同的缩进。
截图 - https://i.stack.imgur.com/Mqp8A.png
片段布局:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list_related"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:paddingTop="20dp"
android:paddingBottom="20dp"
tools:listitem="@layout/item_2_lenta"/>
</LinearLayout>
项目布局:
<com.google.android.material.card.MaterialCardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="4dp"
app:cardBackgroundColor="#ff0">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
...
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
UPD
创建一个布局管理器
vRelatedStub.setOnInflateListener((viewStub, sView) -> {
vLabelRelated = sView.findViewById(R.id.label_related);
vListRelated = sView.findViewById(R.id.list_related);
vLoaderRelated = createLoaderView(sView, mAdapterRelated);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(),
LinearLayoutManager.HORIZONTAL, false);
vListRelated.setLayoutManager(layoutManager);
vListRelated.setAdapter(mAdapterRelated);
});
vRelatedStub.inflate();
【问题讨论】:
-
这可能是因为您正在根据内容包装 MaterialCardView 的高度。因此,从您提供的屏幕截图中我可以看到,由于第二项具有三行文本,因此它具有更大的高度。结果,您会看到不同高度的卡片。解决方案是为保存文本的 textview 分配“minLine”和“maxLine”值。这样,无论内容大小如何,textview 都将在整个 recyclerview 中具有统一的高度。
-
@Azhar92 其实到处都是3行,但是即使我给MaterialCardView设置了一个固定的高度,问题依然存在。 i.stack.imgur.com/xprid.png
-
你能分享你为布局管理器赋值的代码吗?
-
@Shubham 我在描述中添加了创建代码
标签: android android-linearlayout horizontal-scrolling