29

I'm facing a tricky situation here and I don't know how to solve this problem.
In my project I have a custom BottomSheetDialogFragment and in the layout a FrameLayout to add or replace Fragments.

Now I have a Fragment and inside I have a RecyclerView with the height:="wrap_content" because I want the BottomSheetDialogFragment only use the necessary space. Everything looks great, the problem appear when I put another view inside of the same layout and set the RecyclerView bellow or above of that view.
The RecyclerView ignores the size of the other view (or views) and always grows to the max screen size, and then it's no possible to see a few elements and even scroll.

I saw a solution, some developers are suggesting to add paddingBottom equals to the height of the view. But in my case doesn't works because I want to have a dynamic solution.

Above I'll share a few images of the problem and GitHub Repository with a sample.

enter image description here enter image description here Thanks for your attention!

extmkv
  • 1,991
  • 1
  • 18
  • 36

2 Answers2

27

I've manage to do what you need just need to use this as your fragment_sample.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
android:orientation="vertical">

<android.support.v7.widget.RecyclerView
    android:id="@+id/rclItems"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>

<Button
    android:id="@+id/btnAddMoreItems"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/rclItems"
    android:text="@string/add_1_item"/>

</LinearLayout>

Explanation Using a LinearLayout gives you the ability to work with weight, and the vertical orientation allows you to place an item below the other. The weight on the recyclerview will increase the height of it as needed until filling the screen. The next item you add would be added to the recyclerview but you'll need to scroll the list to see it

sebasira
  • 1,739
  • 1
  • 22
  • 41
  • 3
    Yes, it's working, but I still confusing... do you know why work in this way and not with `wrap_content` inside of a `ReleativeLayout` or in a `ConstraintLayout`? – extmkv Sep 13 '18 at 14:36
  • 1
    Yes, because with relative or constraint the button will always be below the recyclerview. So if it has 100 records below the 100th will be the button. The way I propose you, you are forcing the button to be visible and let the recycler view expand as mush as needed, until reaching the screen height. Then it could still grow but as the button is always visible, you will need to scroll the recycler to view them. That's how the **weight** works – sebasira Sep 14 '18 at 01:52
  • 1
    Yes, make all the sense @sebasira! Thanks for your explanation! – extmkv Sep 14 '18 at 09:07
  • 1
    @sebasira I don't know why the hell but this works perfectly and fixes all the issues that we had with `RecyclerView` in `BottomSheetDialogFragment`. Only with Android, you can fix a bunch of issues by only replacing the parent layout :) – amatkivskiy Feb 17 '21 at 11:57
  • The permanent and latest solution as of today is this : https://stackoverflow.com/a/76591005/5471183 – Hitesh Sarsava Jun 30 '23 at 17:10
-5

The android developers blog says that :-

The scrolling containers in your bottom sheet must support nested scrolling .

Try changing your fragment_sample.xml as below to make the recyclerview scroll working and to make the add button persistent.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="wrap_content">

 <android.support.v4.widget.NestedScrollView
 android:layout_width="match_parent"
 android:id="@+id/next"
 android:layout_above="@id/btnAddMoreItems"
 android:layout_height="wrap_content">

<android.support.v7.widget.RecyclerView
    android:id="@+id/rclItems"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />
</android.support.v4.widget.NestedScrollView>
   <Button
    android:id="@+id/btnAddMoreItems"
    android:layout_width="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_height="wrap_content"
    android:text="@string/add_1_item"/>
</RelativeLayout>

Note: making bottomsheet layout a child view of CoordinatorLayout will allow you to get the implement BottomSheetBehavior and recieve its transitions callbacks .

  • 4
    Don't put RecyclerView into a scroll view. RecyclerView supports nested scrolling. `The android developers blog says` Always post a link to back up your claims. – Eugen Pechanec Feb 07 '19 at 14:36