Previewing horizontal recyclerview in android studio
AndroidAndroid StudioAndroid RecyclerviewAndroid Problem Overview
I found out how to preview a list item by using
tools:listitem="@layout/my_item_layout"
Android studio however is previewing the recyclerview as a vertical list. Is there a way to tell Android Studio to display the layout preview in a horizontal fashion using LinearLayoutManager
?
Android Solutions
Solution 1 - Android
Add a LayoutManager and set a horizontal orientation.
Here an example:
<android.support.v7.widget.RecyclerView
android:id="@+id/homesRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
android:layout_centerVertical="true"
/>
Solution 2 - Android
If you are using androidx libraries:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/homesRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:layout_centerVertical="true"
/>
Solution 3 - Android
Just use in your layout the app:layoutManager
and the android:orientation
attributes and add them also using the tools
namespace.
Something like:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/ly_item"
tools:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:orientation="horizontal"
../>
Solution 4 - Android
the accepted answer works accurately. For androidx just use this in your recyclerView
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" android:orientation="horizontal"
Solution 5 - Android
Otherwise you can use in java file :
mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true));