2

I have an Android app using a recyclerView and a LinearLayoutManager. I was under the impression that changing the LinearLayoutManager to GridLayoutManager (see the commented line below and the following line) will simply give me a grid. But nothing changes and I still see the linear list, one item per row, and one column.

    recyclerView = view.findViewById(R.id.item_setup_recycler_view);
    recyclerView.setHasFixedSize(true);
    //recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
    adapter = new ItemSetupRecyclerViewAdapter(getActivity(), kids);
    recyclerView.setAdapter(adapter);

Can someone please let me know if there are other areas I need to change before I see the grid?

Here is the xml for the recyclerView:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/feed_recycle_view"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    android:clipToPadding="false"
    android:paddingBottom="74dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView8"
    app:layout_constraintVertical_bias="0.0">

</androidx.recyclerview.widget.RecyclerView>

Thanks in advance

LeaningAndroid
  • 445
  • 2
  • 4
  • 12

3 Answers3

0

Use this like this. In your .xml

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

In your code.

GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(),3);

gridLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);

then give this to your recyclerView.

Or you can do with one line:

recyclerView.setLayoutManager( new GridLayoutManager(getApplicationContext(),3,LinearLayoutManager.HORIZONTAL,false);)
0

you need to add orientation and direction of the list

recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),2, RecyclerView.VERTICAL, false));
0

Thanks to the comment from @Công Hải I realized the change I was doing was the only change needed. The issue was I made the change on the wrong recycleView in my app. Once I modified the right recycleView everything worked as expected.

LeaningAndroid
  • 445
  • 2
  • 4
  • 12