Shah Alam
Shah Alam

Reputation: 23

Orientation is not working in recyclerview in BottomNavigationView Fragment. Where to set orientation attribute?

I have a RecyclerView in a fragment and want to set orientation horizontal. But adding attribute android:orientation="horizontal" is not working.Here are the layouts.

Fragment Layout:

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  <android.support.v7.widget.RecyclerView
    android:id="@+id/dashboardRecyclerView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

</android.support.v7.widget.RecyclerView>

Model Layout:

    <android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_margin="10dp"
    app:cardCornerRadius="5dp"
    app:cardElevation="5dp">

            <TextView
                android:id="@+id/textViewId"
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:padding="10dp"
                android:text="@string/name"
                android:textAppearance="@style/TextAppearance.AppCompat.Large" />
</android.support.v7.widget.CardView>

where should I put android:orientation="horizontal"attribute? Thanks

Upvotes: 0

Views: 1063

Answers (2)

kfir88
kfir88

Reputation: 380

The "orientation" attribute in xml is for LinearLayout and other. for a recyclerview, the orientation set by the LayoutManager Objet

in your Activity or Fragment do this for your recyclerView

RecyclerView yourecyclerView =RecyclerView)findViewById(R.id.dashboardRecyclerView);
  LinearLayoutManager layout = new LinearLayoutManager(getActivity(),     LinearLayoutManager.HORIZONTAL, false);
    yourecyclerView.setLayoutManager(layout);

Upvotes: 1

Udit
Udit

Reputation: 1037

If you want a horizontal list with recyclerview, you need to set the orientation in the LayoutManager.

eg:

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.dashboardRecyclerView)
LinearLayoutManager layoutManager
    = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(layoutManager);

If you would rather do this in xml, you need to specify the layout manager as well in xml:

<android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:paddingBottom="@dimen/spacing_regular"
            android:clipToPadding="false"
            android:paddingTop="@dimen/spacing_regular"
            android:scrollbars="none"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager" />

Upvotes: 3

Related Questions