LinearLayoutManager(LinearLayoutManager.HORIZONTAL)不起作用

时间:2019-10-23 17:46:58

标签: android android-linearlayout horizontal-scrolling

我想使用水平滚动回收器。

为此,我使用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

创建layoutManager

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();

1 个答案:

答案 0 :(得分:0)

在我的代码中发现错误。 事情是我要让装订支架中的第一项有余量, 这仍然来自垂直线性经理。