Search code examples
androidxmlkotlinandroid-layout

Dynamic height layout with RecyclerView inside


everyone.

I'm struggling with a layout here where I have a component made with ConstraintLayout. Inside this component, I have a fixed title and a RecyclerView right below, which is supposed to scroll respecting this title.

When my component has up to 6 elements, it has a wrap_content to match the size of the elements. If it has more than 6 elements, I have fix the component size to 1000px.

The problem is: When I have more than 6 elements, the RecyclerView is not been displayed correctly. The last elements are cutted off. By my understanding, looks like the view of the component has a bigger size than it looks when rendering, which is making me crazy to fix it.

Here is my component code:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout     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:id="@+id/component_card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/background_card_with_radius"
    android:maxHeight="1000px"
    app:layout_constraintHeight_max="1000px"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    android:paddingStart="16px"
    android:paddingEnd="16px"
    android:paddingTop="32px"
    android:paddingBottom="32px"
    android:clipToPadding="false"
    tools:ignore="HardcodedColorValue">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/StandardText"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:textAlignment="viewStart"
        android:text="@string/title_hdr" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/list_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:nestedScrollingEnabled="false"
        app:layout_constraintTop_toBottomOf="@+id/title"
        app:layout_constraintStart_toStartOf="parent"
        app:divider_visibility="gone"
        android:scrollbars="vertical"
        android:fadeScrollbars="false"
        android:scrollbarStyle="outsideOverlay"
        app:layout_constrainedHeight="true" />

</androidx.constraintlayout.widget.ConstraintLayout>

And here is the code to call it:

<components.CardComponent
        android:id="@+id/card_component"
        android:layout_width="@dimen/card_width"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginStart="@dimen/margin_small"
        android:visibility="gone"
        android:clipToPadding="false" />

I've already tried to create a NestedScrollView around the RecyclerView, also to set an extra padding in the parent layout and other many things. But the main idea was to make the RecyclerView scrollable and the component be with a dynamic height with less than 6 items.

The expected: enter image description here

Can anyone can give some light on that?

Thanks in advance!


Solution

  • used this xml code

    <?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".presentation.activities.SeeActivity">
    <LinearLayout
        android:id="@+id/box_one"
        android:layout_width="match_parent"
        android:layout_height="90dp"
        android:layout_alignParentBottom="true"
        android:orientation="vertical"
        android:gravity="center">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:text="Rahul Rokade"
            android:gravity="center"
            android:textStyle="bold"
            android:textColor="@color/black"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:text="8999539845"
            android:gravity="center"
            android:textStyle="bold"
            android:textColor="@color/black"/>
    </LinearLayout>
    <RelativeLayout
        android:layout_above="@id/box_one"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="60dp">
        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:cardBackgroundColor="@color/white"
            android:layout_margin="10dp"
            app:cardCornerRadius="10dp"
            app:cardElevation="10dp"
            android:elevation="10dp"
            android:padding="10dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Title"
                    android:textColor="@color/black"
                    android:textStyle="bold"
                    android:textSize="14sp"
                    android:padding="5dp"
                    android:layout_marginStart="10dp"/>
                <androidx.recyclerview.widget.RecyclerView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:scrollbars="vertical"
                    android:layout_marginVertical="10dp"
                    android:layout_marginStart="10dp"
                    android:scrollIndicators="right"
                    />
            </LinearLayout>
        </androidx.cardview.widget.CardView>
    </RelativeLayout>