Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make recycler view start adding items from center?

  • I have a recyclerView with "Horziontal Linear Layout" as layout manager.
  • Recycler View is in a frame layout, with layout_gravity = "center", and layout_width="wrap_content"
  • I want recycler view start adding items from center.
  • Here is what I want:

enter image description here

enter image description here

enter image description here

  • And Here is what I am getting:

enter image description here

  • You can see that in the last image items are added from left. I want it to add items from center as shown in the first three images .
like image 983
Muhammad Sadiq Alvi Avatar asked May 15 '15 12:05

Muhammad Sadiq Alvi


People also ask

How do I center items in a RecyclerView?

Take LinearLayout in your RecyclerView 's item row layout then give android:layout_gravity="center" to LinearLayout .

How do you tell the RecyclerView to start at a specific item position?

If onLayoutChildren is called by RecyclerView, it checks if adapters itemCount is already > 0. If true, it calls scrollToPositionWithOffset() . So I can tell immediately what position should be visible, but it will not be told to LayoutManager before position exists in Adapter. Show activity on this post.

Is ListView better than recycler?

Simple answer: You should use RecyclerView in a situation where you want to show a lot of items, and the number of them is dynamic. ListView should only be used when the number of items is always the same and is limited to the screen size.


2 Answers

i had same issue and solved like this for my horizontal RecyclerView:

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="wrap_content"
        android:layout_height="68dp"
        android:layout_centerHorizontal="true"
        android:orientation="horizontal"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager" />

Here the main part is android:layout_centerHorizontal="true" and layout_width="wrap_content"

like image 54
Syed Arsalan Shah Avatar answered Sep 24 '22 07:09

Syed Arsalan Shah


Solved:

How do I make WRAP_CONTENT work on a RecyclerView

This was the solution, I had to enforce the wrap_content for recycler view, So a custom Layout manager worked for me. Hope this will help others:)

like image 28
Muhammad Sadiq Alvi Avatar answered Sep 23 '22 07:09

Muhammad Sadiq Alvi