简体   繁体   English

无法让 RecyclerView 水平显示 Android Java

[英]Can't get RecyclerView to display Horizontally Android Java

I'm trying to get my planner_day RecyclerView to display horizontally.我试图让我的 planner_day RecyclerView水平显示。 After adding添加后

app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:orientation="horizontal"

It still won't display horizontally and looks like this:它仍然不会水平显示,看起来像这样:

在此处输入图像描述

After playing around with the layout_height and layout_width , I've still had no success.在玩了layout_heightlayout_width之后,我仍然没有成功。

RecyclerView xml: RecyclerView xml:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/planner_day"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            android:orientation="horizontal"
            android:padding="4dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/viewCities"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            android:padding="4dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/planner_day"
            app:layout_constraintVertical_bias="0.108" />
    </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>
</layout>

Items xml项目 xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">


    <Button
        android:id="@+id/planner_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

When you use a RecyclerView , you need to specify a LayoutManager that is responsible for laying out each item in the view.当您使用RecyclerView时,您需要指定一个LayoutManager负责布局视图中的每个项目。 The LinearLayoutManager allows you to specify an orientation, just like a normal LinearLayout would. LinearLayoutManager允许您指定方向,就像普通的LinearLayout一样。

To create a horizontal list with RecyclerView , you might do something like this:要使用RecyclerView创建水平列表,您可以执行以下操作:

In your activity/fragment, you can simply do the following:在您的活动/片段中,您可以简单地执行以下操作:

LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);

RecyclerView plannerDayRv = (RecyclerView) findViewById(R.id.planner_day);
plannerDayRv.setLayoutManager(layoutManager);

Better to remove this unused code from RecyclerView.xml :最好从RecyclerView.xml中删除这个未使用的代码:

 app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
 android:orientation="horizontal"

To achieve that create and set LinearLayoutManager programatically instead of setting that from xml .为了实现这一点,以编程方式创建和设置LinearLayoutManager而不是从xml设置。 Here is the solution.这是解决方案。

Remove this two lines from your xmlxml删除这两行

app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:orientation="horizontal"

Set it programatically以编程方式设置它

RecyclerView plannerDayRv = (RecyclerView) findViewById(R.id.planner_day);
LinearLayoutManager layoutManager
                    = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
myRecyclerView.setLayoutManager(layoutManager); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM