Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView (wrap_content) inside of a BottomSheetDialogFragment

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!

like image 722
extmkv Avatar asked Sep 05 '18 14:09

extmkv


1 Answers

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

like image 179
sebasira Avatar answered Nov 09 '22 12:11

sebasira