Не удается получить RecyclerView для горизонтального отображения Android Java - PullRequest
1 голос
/ 13 апреля 2020

Я пытаюсь заставить мой planner_day RecyclerView отображаться горизонтально. После добавления

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

он все равно не будет отображаться горизонтально и выглядит следующим образом:

enter image description here

После игры с layout_height и layout_width, я до сих пор не добился успеха.

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 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>

Ответы [ 2 ]

2 голосов
/ 13 апреля 2020

Когда вы используете RecyclerView, вам нужно указать LayoutManager, который отвечает за размещение каждого элемента в представлении. LinearLayoutManager позволяет вам указывать ориентацию, так же, как это делает нормальный LinearLayout.

Чтобы создать список горизонтальный с RecyclerView , вы можете сделать что-то вроде этого:

В вашей деятельности / фрагменте вы можете просто сделать следующее:

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

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

Лучше удалить этот неиспользуемый код из RecyclerView. xml:

 app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
 android:orientation="horizontal"
1 голос
/ 13 апреля 2020

Для этого создайте и установите LinearLayoutManager программно, а не xml. Вот решение.

Удалите эти две строки из вашего xml

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

Установите его программно

RecyclerView plannerDayRv = (RecyclerView) findViewById(R.id.planner_day);
LinearLayoutManager layoutManager
                    = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
myRecyclerView.setLayoutManager(layoutManager); 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...