Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjust items height to fit its RecyclerView according to screen size

I have a RecyclerView with height:match_parent which displays 16 items vertically with fixed height in adapter. Everything works fine except when it comes to the different screen sizes. In larger screens there is a space remained at the bottom after 16 items are displayed, where on small screen phones it perfectly fits to the bottom. I am looking for a way where list items heights are adjusted to the RecyclerView height till the end without scrollable.

Check the Screenshots below for more clarification

Problem:

enter image description here

Expectation:

enter image description here

My approach was to make the Item weight set to 1, hence when rendering into the RecyclerView 16 times, they will share the same height within the RecyclerView. Unfortunately that didn't work for me.

My RecyclerView:

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

My Adapter:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@color/white"
    android:clickable="false"
    android:orientation="vertical">

    <TextView />

</LinearLayout>
like image 642
VipiN Negi Avatar asked Nov 17 '22 15:11

VipiN Negi


1 Answers

The exact solution for your problem is the flexible layout manager for ReyclerView.

More about flexible layouts.

An example illustrating the use of flexible layouts.

like image 66
Nitin Gurbani Avatar answered Jan 16 '23 02:01

Nitin Gurbani