LuqmanChohan8001
LuqmanChohan8001

Reputation: 9

Recycler View Horizontal Android

I've made two recycler views in one layout. I am facing an issue that my horizontal recycler view is shown as vertical view I've uploaded my xml file.

The vertical recycler view is working correctly, but the horizontal recycler view has a problem that it shows items vertically.

Horizontal.xml


 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="vertical"
 >
 <TextView
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:hint="City"
     android:textStyle="bold"
     android:background="@color/LightGreen"
     />

 <androidx.recyclerview.widget.RecyclerView
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:id="@+id/city_recycleview"
     />
</LinearLayout>

Horizontal_single_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=
    "http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
>

<androidx.cardview.widget.CardView
    android:layout_width="120dp"
    android:layout_height="120dp"
    app:cardCornerRadius="4dp"
    app:cardElevation="1dp"
    app:cardMaxElevation="2dp"
    android:layout_centerInParent="true"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_margin="1dp"
    >


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <de.hdodenhof.circleimageview.CircleImageView
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_marginTop="5dp"
            android:layout_gravity="center"
            android:scaleType="centerCrop"
            android:layout_centerHorizontal="true"
            android:id="@+id/city_imagecicrlemain"
            android:src="@drawable/ic_launcher_background"
            >

        </de.hdodenhof.circleimageview.CircleImageView>

        <TextView
            android:id="@+id/name_Citymain"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_below="@id/city_imagecicrlemain"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="4dp"
            android:layout_marginBottom="5dp"
            android:autoSizeMaxTextSize="15sp"
            android:autoSizeMinTextSize="8sp"
            android:autoSizeTextType="uniform"
            android:text="Lahore"
            android:textColor="@color/black">
            </TextView>

    </RelativeLayout>

</androidx.cardview.widget.CardView>
</LinearLayout>


Upvotes: 0

Views: 162

Answers (2)

dr4gon
dr4gon

Reputation: 23

The Orientation is controlled by your layout manager, make sure you have its orientation set to HORIZONTAL, I would recommend using LinearLayoutManager for this use case.

in Kotlin:
//In your activity / fragment

val linearLayoutManager = LinearLayoutManager(
            context,
            LinearLayoutManager.HORIZONTAL,
            false
        )

    yourRecyclerView.layoutManager = linearLayoutManager

Hope it solves your problem :)

Upvotes: 0

gkpln3
gkpln3

Reputation: 1499

To make your RecyclerView horizontal, you need to set it's layoutManager.

final GridLayoutManager layoutManager = new GridLayoutManager(this, NUM_OF_ROWS, GridLayoutManager.HORIZONTAL, false);
m_recyclerView.setLayoutManager(layoutManager);

Upvotes: 1

Related Questions