Center vertically RecyclerView in fragment

Firstly, I'm a little new to Android. I currently have an activity that loads a fragment, and in this fragment I created an instance of 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 snippet:

<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 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 several hours to vertically center my RecyclerView without success (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 time :)

+4
source share
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>
+7
source

, , :

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();
            }
        }
    });

/ fill_parent center.

+1

All Articles