android - RecyclerView 未使用 ConstraintLayout Group Visibility 正确更新 View

标签 android android-constraintlayout

问题:

似乎当我在 Visible 和 Gone 之间有特定的 View 和 setVisibility 组合时,RecyclerView 在初始加载后无法正确更新。我有一个 RelativeLayout->ConstraintLayout->Constraint Group(可见性动态设置)。约束组内的 View 未正确更新。

问题用例示例:

所以下面的链接代码将在顶部显示一个搜索 View 。空的初始状态正确显示 View (显示搜索图标)。然后,如果您键入“T”,搜索图标就会消失(它不应该)。因此,如果您删除 T 或键入下一个字母“E”,它会再次显示。此外,如果您删除所有搜索文本并再次键入 T,它将显示。

查看代码:

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

<data>

    <variable
        name="vm"
        type="com.example.davidcorrado.myapplication.PlayerVM" />
</data>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/playerCell_main_layout"
        android:layout_width="match_parent"
        android:layout_height="84dp"


        android:layout_alignParentTop="true">

        <android.support.constraint.Group
            android:id="@+id/playerCell_status_group"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="@{vm.showingStatusIcons}"

app:constraint_referenced_ids="playerCell_lineup_status_image" />

        <ImageView
            android:id="@+id/playerCell_lineup_status_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:src="@drawable/ic_search" />


    </android.support.constraint.ConstraintLayout>


</RelativeLayout>
</layout>

可能有帮助的怪癖:

1) 如果我将可见性线移动到 relativeLayout 或 ConstraintLayout,一切似乎都有效

2) 如果我删除 RelativeLayout View ,一切似乎都有效。

完整代码示例见下方: https://github.com/DavidCorrado/LayoutIssue

最佳答案

据我所知,搜索图标消失的原因是调用 ObservableList.clear() 然后调用 ObservableList.addAll(*)

所以消失发生在 clear 上,然后重新出现发生在 addAll 上。 当适配器中的回调在两个调用中启动时,我的直觉是它使 View 动画消失,然后在触发 addAll 时动画显示。

我已经通过不执行“List.clear()”来验证这一点,而是在您的 textChange 监听器中简单地向列表添加更多 View 。

我不确定您如何在不对清除进行动画处理的情况下清除项目并将其添加到列表中,也许您可​​以在 RecyclerAdapter 中切换一些设置以忽略 View 的删除或不对 View 的条目进行动画处理?

我在类 PlayersListVM 回调中调整的代码

class PlayersListVM {
    val filteredVms: ObservableList<ViewModel> = ObservableArrayList<ViewModel>()
    val showing = PlayerVM(isShowing = true)
    val missing = PlayerVM()

    init {
        filteredVms.add(showing)
    }

    fun onQueryChanged(newText: String) {
        if (filteredVms.size % 2 == 1) filteredVms.add(missing)
        else filteredVms.add(showing)
    }
}

关于android - RecyclerView 未使用 ConstraintLayout Group Visibility 正确更新 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51142296/

相关文章:

android - 是否可以将 ConstraintLayout 放在 ScrollView 中?

android - 在线性布局android中平滑地动画 View 隐藏/显示

android - 应用程序在 android 中启动

android - "Allow all the time"Android SDK 29 中没有位置提示

java - 安卓 : Constraint Layout Set Constraint top programmatically?

Android Studio 3 - 约束布局编辑器不工作

Android约束布局包括另一个布局

java - 如何从firebase数据库中获取基于值的数据

android - 当默认设置为 0 时,SharedPreferences getInt 返回 -1

弹出键盘时Android Constraint布局底部元素重叠