Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center vertically RecyclerView in fragment

Firstly, I'm a bit of a novice in Android. I currently have an activity which load a fragment and in this fragment, I have instantiated a RecyclerView (with horizontal orientation).

Here are my current layouts:

My main:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/red"
    android:orientation="vertical">
        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/green">
        </FrameLayout>
</LinearLayout>

My fragment :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="vertical"
    android:background="@color/yellow">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/fragment_atelier_recycler_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerInParent="true"
        android:background="#FFE5E5E5"
        android:scrollbarStyle="insideOverlay"
        />

</LinearLayout>

My item layout for my RecyclerView adapter :

    <?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:background="@color/gray">

    <ImageView
        android:id="@+id/item_icon"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="1dp"
        android:layout_marginBottom="1dp"
        android:layout_gravity="center"
        android:contentDescription="icon"/>
</LinearLayout>

I tried for a few hours to center vertically my RecyclerView without success (by using gravity, layout_centerVertical, etc...). Maybe I missed something about wrap_content/match_parent or Linear/Relative specs.

Thanks in advance and have a good day :)

like image 879
Kupris Avatar asked Jun 24 '15 19:06

Kupris


2 Answers

This should do the trick:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@color/yellow">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/fragment_atelier_recycler_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="#FFE5E5E5"
        android:scrollbarStyle="insideOverlay"
        />

</RelativeLayout>
like image 138
jmateo Avatar answered Sep 30 '22 13:09

jmateo


This is what I did to center the items vertically:

mRecyclerViewView.addItemDecoration(new RecyclerView.ItemDecoration() {
        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            try {
                RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();
                params.height = parent.getHeight();
                view.setLayoutParams(params);
            } catch (NullPointerException e) {
                e.printStackTrace();
            }
        }
    });

Then in my item layout, I set the child view's width/height to fill_parent and gravity to center.

like image 37
Ahmadul Hoq Avatar answered Sep 30 '22 11:09

Ahmadul Hoq