bateler8
bateler8

Reputation: 105

Can't get RecyclerView to display Horizontally Android Java

I'm trying to get my planner_day RecyclerView to display horizontally. After adding

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

It still won't display horizontally and looks like this:

enter image description here

After playing around with the layout_height and layout_width, I've still had no success.

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>

Upvotes: 1

Views: 319

Answers (2)

Waqar UlHaq
Waqar UlHaq

Reputation: 6422

When you use a RecyclerView, you need to specify a LayoutManager that is responsible for laying out each item in the view. The LinearLayoutManager allows you to specify an orientation, just like a normal LinearLayout would.

To create a horizontal list with RecyclerView, you might do something like this:

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:

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

Upvotes: 2

Tiko
Tiko

Reputation: 851

To achieve that create and set LinearLayoutManager programatically instead of setting that from xml. Here is the solution.

Remove this two lines from your xml

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

Upvotes: 1

Related Questions