Search code examples
androidandroid-fragmentsandroid-recyclerviewandroid-viewpager

How set dynamic height of fragment contains RecycleView in ViewPager android?


I got help from links and other links but it didn't help me and my problem was not solved

MY PROBLEM: I have ViewPager with 3 Fragment that 2 of them contain RecycleView with dynamic data!

Now the ViewPage gets the biggest height !!!! I want to set the height of the fragment as the original size!

my code:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    style="@style/CardStyle"
    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">

    <com.duolingo.open.rtlviewpager.RtlViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_marginTop="@dimen/default_margin_double"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tab_layout" />

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tab_layout"
        style="@style/TabbedCard"
        android:layout_width="match_parent"
        android:layout_height="@dimen/tabbed_card_tab_layout_height"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:tabGravity="fill"
        app:tabMode="scrollable"
        app:tabPaddingTop="@dimen/default_margin_half"
        app:tabTextAppearance="@style/investTab" />


</androidx.constraintlayout.widget.ConstraintLayout>

and one of my fragment that contains RecyclerView :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:scrollbarStyle="insideOverlay"
    android:orientation="vertical"
    tools:context=".mvvm.screens.investment.view.fragment.tabs.RequestListFragment">


 

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

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/account_list"
            style="@style/InvestmentRecyclerView"
            android:scrollbarStyle="insideOverlay"
            tools:listitem="@layout/investment_request_item"
            android:paddingBottom="@dimen/default_margin"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ProgressBar
            android:id="@+id/loading"
            style="?android:attr/progressBarStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:indeterminateTint="?attr/cardTitle"
            android:visibility="gone"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


        <include
            android:id="@+id/view_error"
            layout="@layout/view_error"
            android:visibility="gone" />

        <include
            android:id="@+id/view_empty"
            layout="@layout/no_investment_found"
            android:visibility="gone" />

    </androidx.constraintlayout.widget.ConstraintLayout>


</LinearLayout>

Solution

  • Finally, I Create custom class from ViewPager like this :

    public class WrappedHeightTabbedCardViewPager extends NonSwipeableViewPager {
        
            private int currentPagePosition = 0;
        
            public WrappedHeightTabbedCardViewPager(@NonNull Context context) {
                super(context);
            }
        
            public WrappedHeightTabbedCardViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
                super(context, attrs);
            }
        
            @Override
            protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
                setMeasuredDimension(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
                View child = getChildAt(currentPagePosition);
                if (child != null) {
                    child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                    int h = child.getMeasuredHeight();
        
                    heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
                }
                if (heightMeasureSpec != 0) {
                    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
                }
        
            }
        
            public void reMeasureCurrentPage(int position) {
                currentPagePosition = position;
                requestLayout();
            }
        
        }