我有2个回收器视图。1水平和1垂直。在设计页面中,第一个回收器的元素是水平的,第二个是垂直的,但是当我运行时,应用程序两者都是垂直的,我无法找出问题所在。
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/selected_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/name_edit_text"
tools:listItem="@layout/item_participant_selected" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="120dp"
android:layout_marginBottom="@dimen/participants_padding_bottom"
android:clipToPadding="false"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintEnd_toEndOf="@id/selected_recycler_view"
app:layout_constraintStart_toStartOf="@id/selected_recycler_view"
app:layout_constraintTop_toBottomOf="@id/selected_recycler_view"
tools:listItem="@layout/item_participant" />
xml-
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
程序上-
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(layoutManager);
它可以很容易地通过编程方式完成
XML:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="120dp"
android:layout_marginBottom="@dimen/participants_padding_bottom"
app:layout_constraintEnd_toEndOf="@id/selected_recycler_view"
app:layout_constraintStart_toStartOf="@id/selected_recycler_view"
app:layout_constraintTop_toBottomOf="@id/selected_recycler_view"
tools:listItem="@layout/item_participant" />
Java:
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recycler_view.setLayoutManager(layoutManager);
静态编程语言:
recycler_view.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
尝试水平线性布局管理器,因为我已经提交
LinearLayoutManager layoutManager
= new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);