1

我有2 recyclerView,RecyclerView 1,有 layoutManagerapp:layoutManager="com.google.android.flexbox.FlexboxLayoutManager"和另一个 RecyclerView 2,有app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager".

recyclerview在另一个 recyclerView 上滚动时,第一项闪烁。当 RecyclerView 1 的项目数小于 6 或 7 时,它工作正常。

请查看视频链接以获取问题参考(0:18s - 0:46s):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 的 ScrollToPosition,在添加新项目时

bViewDataBinding?.rvConditionsSelected?.scrollToPosition(adapterList.size - 1)
4

1 回答 1

1

我找到了闪烁问题的根本原因,问题在于第一个 recyclerView 高度。所以我通过最初添加一个固定的高度来解决这个问题。如果您在我的查询中看到,recyclerView 的高度是android:layout_height="@dimen/dp_0"&app:layout_constraintHeight_max="@dimen/dp_100"这会导致 FlaxBoxLayoutManager recyclerView 上的闪烁。

<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
}
于 2021-03-07T14:42:13.543 回答