androidandroid-recyclerviewrow-heightexpandablerecyclerview

RecyclerView Two Types item height wrap content


I use two ViewTypes in my adapter to show different layouts:

        case 0:
            return new Holder(LayoutInflater.from(parent.getContext()).inflate(R.layout.faq_item, parent, false));
        case 2:
            return new HolderAnswer(LayoutInflater.from(parent.getContext()).inflate(R.layout.faq_item_answer, parent, false));
        default:
            return null;
    }

By default each second layout has invisible(GONE) TextView which turns Visible on first item click

If I set first item fixed height it works fine, but item can contain long texts sometimes that won't be seen:

<ConstraintLayout
layout_height="@dimen/..."
...>
....
</ConstraintLayout>

If I change height to wrap_content I can watch unexpected behavior: First and second elements has height equals screen height, but after few swipes to the end and to the beginning of the list the height fits to expected measures.

My guess, that it is because of using two view types or wrong alignments. Tried both true/false for setHasFixedSize()Here are layouts:

question.xml

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container_question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#CCEFF5">

<View
    android:id="@+id/indicator"
    android:layout_width="3dp"
    android:layout_height="20dp"
    android:background="#ff008ca5"
    android:visibility="invisible"
    app:layout_constraintBottom_toBottomOf="@+id/question"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="@+id/question" />

<com.hastee.pay.ui.view.Text
    android:id="@+id/question"
    style="@style/black_regular_15"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginStart="20dp"
    android:layout_marginTop="16dp"
    app:infont="bold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/plus"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0" />

<ImageView
    android:id="@+id/plus"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="20dp"
    app:layout_constraintBottom_toBottomOf="@+id/question"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="@+id/question"
    app:srcCompat="@drawable/ic_plus_small" />


</android.support.constraint.ConstraintLayout>

answer.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#CCEFF5">
<com.hastee.pay.ui.view.Text
    android:id="@+id/answer"
    style="@style/black_regular_13"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="16dp"
    android:layout_marginLeft="32dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="16dp"
    android:alpha="0.7" />

<View
    android:id="@+id/divider"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_gravity="bottom"
    android:layout_marginLeft="20dp"
    android:layout_marginStart="20dp"
    android:background="@color/divider" />
</FrameLayout>

Any thoughts?


Solution

  • Solved by simply switching from ConstraintLayout to RelativeLayout. Looks like incorrect work of the first one with complex lists.