Android RecyclerView не обновляет свой вид элемента сразу после добавления / удаления подпредставления в его ViewGroup - PullRequest
0 голосов
/ 07 января 2020

У меня есть ConstraintLayout, который содержит RecyclerView и индикатор загрузки. После щелчка по элементу RecyclerView я добавил подпредставление в ConstraintLayout (ReactionView), а затем мой пользователь может выбрать одну эмоцию в своем.

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>

    <variable
        name="viewModel"
        type="com.sunasterisk.news.ui.widgets.CustomRecyclerView" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical">

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/refresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.sunasterisk.news.ui.widgets.recyclerview.NestedHorizontalRecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white" />

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

    <ProgressBar
        android:id="@+id/progress_load_more"
        android:layout_width="@dimen/dp_20"
        android:layout_height="@dimen/dp_20"
        android:layout_gravity="center"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <LinearLayout
        android:id="@+id/layout_no_data"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:orientation="vertical"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/image_empty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />


        <com.sunasterisk.news.ui.widgets.CustomTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/dp_26"
            android:textSize="@dimen/sp_18" />
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

private fun showReactionView(view: View?) {
    Timber.d("showReactionView()")
    val location = IntArray(2)
    view?.getLocationOnScreen(location)
    val y = location[1]
    Timber.d("reactionView.y: ${y.minus(dimensUtil.REACTION_MINUS_VIEW_TO_TOP)}")
    reactionView = ReactionView(requireContext()).also {
        it.y = y.minus(dimensUtil.REACTION_MINUS_VIEW_TO_TOP).toFloat()
        it.eventController = this
        it.viewBaseLineY = y
    }
    container.addView(reactionView)
    container.also {
        it.reactionView = reactionView
    }
}

После этого, Я удаляю подпредставление и вызываю adapter?.notifyItemChanged(position), но RecyclerView не обновляет этот элемент немедленно. Он обновляется только после первого прикосновения к RecyclerView.

Этот RecyclerView обновляется нормально, если я не добавляю подпредставление в ConstraintLayout.

Как я могу обновить это RecyclerView сразу после удалить подпредставление из ViewGroup (ConstraintLayout)?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...