#android-recyclerview #android-flexboxlayout
#android-recyclerview #android-flexboxlayout
Вопрос:
У меня есть 2 recyclerView
RecyclerView 1, имеющий LayoutManager с app:layoutManager="com.google.android.flexbox.FlexboxLayoutManager"
другим RecyclerView 2, имеющий app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
.
Первый recyclerview
элемент мерцает при прокрутке другого RecyclerView. Он работает нормально, когда количество элементов RecyclerView 1 меньше 6 или 7.
Пожалуйста, проверьте ссылку на видео для ссылки на проблему (0: 18 — 0: 46): https://drive.google.com/file/d/17_wa3vd5H7QKh0fgj6Sh310ZNlt2TeG1/view?usp=sharing
Пожалуйста, найдите фрагмент кода ниже:
activity_personal_activities.xml
<ScrollView
android:id="@ id/scrollView"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_0"
android:fillViewport="true"
app:layout_constraintBottom_toTopOf="@ id/btnSave"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/tvHeaderTitle">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:id="@ id/rvConditionsSelected"
android:layout_width="@dimen/dp_0"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_12"
android:layout_marginBottom="@dimen/dp_20"
android:orientation="horizontal"
android:paddingStart="@dimen/dp_24"
android:paddingEnd="@dimen/dp_0"
app:layoutManager="com.google.android.flexbox.FlexboxLayoutManager"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_max="@dimen/dp_100"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:spanCount="2"
tools:itemCount="4"
tools:listitem="@layout/inflate_conditions_selected" />
<androidx.recyclerview.widget.RecyclerView
android:id="@ id/rvConditions"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_0"
android:layout_marginTop="@dimen/dp_7"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/rvConditionsSelected"
tools:itemCount="5"
tools:listitem="@layout/inflate_conditions" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
Настройка адаптера:
(bViewDataBinding?.rvConditionsSelected?.itemAnimator as? DefaultItemAnimator)?.supportsChangeAnimations = false
bViewDataBinding?.rvConditionsSelected?.adapter = adapter
Положение прокрутки RecyclerView 1 при добавлении нового элемента
bViewDataBinding?.rvConditionsSelected?.scrollToPosition(adapterList.size - 1)
Ответ №1:
Я нашел основную причину проблемы с мерцанием, и проблема была с первой высотой RecyclerView. Итак, я исправил эту проблему, добавив изначально фиксированную высоту. Если вы видите в моем запросе, высота RecyclerView android:layout_height="@dimen/dp_0"
равна amp; app:layout_constraintHeight_max="@dimen/dp_100"
, что вызывает мерцание в RecyclerView FlaxBoxLayoutManager.
<androidx.recyclerview.widget.RecyclerView
android:id="@ id/rvConditionsSelected"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_36"
android:layout_marginTop="@dimen/dp_12"
android:orientation="horizontal"
app:layoutManager="com.google.android.flexbox.FlexboxLayoutManager"
android:paddingStart="@dimen/dp_24"
android:paddingEnd="@dimen/dp_0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/cvSearch"
tools:itemCount="4"
tools:listitem="@layout/inflate_conditions_selected" />
Итак, я программно изменяю высоту RecyclerView
override fun updateRecyclerViewHeight() {
val flexSize = (bViewDataBinding?.rvConditionsSelected?.layoutManager as? FlexboxLayoutManager)?.flexLinesInternal?.size
?: return
when (flexSize) {
3 -> changeRecyclerViewHeight(SizeUtils.dp2px(this,100F))
2 -> changeRecyclerViewHeight(SizeUtils.dp2px(this,72F))
1 -> changeRecyclerViewHeight(SizeUtils.dp2px(this,36F))
}
}
override fun changeRecyclerViewHeight(height: Int) {
val params = bViewDataBinding?.rvConditionsSelected?.layoutParams
params?.height = height
bViewDataBinding?.rvConditionsSelected?.layoutParams = params
}