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 :)
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>
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With