0

I have a viewpager2 which contains 2 fragments. Both fragments include a single recyclerview and show a list of 4 items (first fragment) and 5 items (second fragment). The problem I encounter is, that my dialogfragment (which has the viewpager2) doesn't expand to the full desired height of the second fragment. Because of this, I can't see every element of my second fragment recycleview and have to scroll down in order to see the last element.

How is it possible to change the dialogfragment height in order to see every item of the second / the fragment with the most items. I want the height of the viewpager2 to be the same height of the fragment which contains the most items. Currently, it is the oppposit.

DialogFragment (Viewpager2 holder)

<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
    android:orientation="vertical">

    <View
        android:id="@+id/emptyView"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="@color/color_primary_dark"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/shop_filter_tablayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/emptyView" />

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/shop_filter_viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="24dp"
        app:layout_constraintBottom_toTopOf="@+id/btn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/shop_filter_tablayout" />

    <com.google.android.material.appbar.MaterialToolbar
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="@color/color_shop_filter_btn_background"
        app:layout_constraintBottom_toBottomOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.google.android.material.button.MaterialButton
                android:id="@+id/shop_filter_submit_filter"
                style="@style/Widget.MaterialComponents.Button.TextButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="8dp"
                android:text="@string/shop_filter_submit"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <com.google.android.material.button.MaterialButton
                android:id="@+id/shop_filter_dismiss_filter"
                style="@style/Widget.MaterialComponents.Button.TextButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="16dp"
                android:text="@string/shop_filter_delete"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@+id/shop_filter_submit_filter"
                app:layout_constraintTop_toTopOf="parent" />

        </androidx.constraintlayout.widget.ConstraintLayout>
    </com.google.android.material.appbar.MaterialToolbar>

</androidx.constraintlayout.widget.ConstraintLayout>

Fragment inside viewpager 2

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_shop_filter"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:listitem="@layout/shop_filter_list_item" />

    <ProgressBar
        android:id="@+id/shop_filter_progress_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:visibility="invisible" />

</androidx.constraintlayout.widget.ConstraintLayout>

First Fragment List (4 items)

enter image description here

Second Fragment list (5 items)

enter image description here

Andrew
  • 4,264
  • 1
  • 21
  • 65
  • try to remove `layout_marginBottom` in `androidx.viewpager2.widget.ViewPager2` may be margin is there. – Jatin Jan 11 '21 at 13:55
  • Nah, didn't changed anything. The second recyclerview is still scrolling. – Andrew Jan 11 '21 at 13:57
  • 1
    There are a few suggestions here: https://stackoverflow.com/questions/58492796/viewpager2-with-differing-item-heights-and-wrap-content As far as I know, the issue is that the first layout happens with the first fragment, so it fits to that size. If you have an arbitrary number of fragments in the adapter, the system would have no way of knowing which is the biggest without inflating all of them to check. Another approach you could try is setting the adapter, setting the current fragment to (index of biggest one) so that gets laid out, and then set it back to index 0 before the user sees it – cactustictacs Jan 11 '21 at 14:41
  • 1
    Applying `offscreenPageLimit = 2` to my viewpager2 did the job for me, I am fine with the in the first fragment since that is actually what I wanted to achieve. Thank you very much – Andrew Jan 11 '21 at 14:57
  • @cactustictacs EDIT: Unfortunately, offscreenPageLimit has a bug for me. It works perfectly fine at the emulator (100 out of 100), but only partially works on my own mobile device (s7). It only works the first time I open the viewpager (height is now correctly set). But after I close the dialogfragment and open it again, the size is back to its normal size (smaller than before) – Andrew Jan 11 '21 at 20:22

1 Answers1

0

I have few ideas:

  1. Try to 'force' change heigh of dialog fragment. Something like that: How to set DialogFragment's width and height?

  2. Try to set SecondFragmet's recycler view heigh as 5xheigh of our item.

  3. Dont use recycler view if there are always 5 items ?? ;)

Give me a sign if any of these will work.

rogalz
  • 80
  • 8
  • What exactly do you mean with "try to set secondfragments recyclerview height as 5x height of our item? And to the third point: There are not always 5 items in my recyclerview. I get the items from my service and there could also be 10 items in my first fragment and only 5 in the second one. So I have to use recyclerview :) – Andrew Jan 11 '21 at 14:41
  • Sorry for my misunderstanding, you wrote " list of 4 items and 4 items" so I assumed these are constans. – rogalz Jan 11 '21 at 14:54