从 DataBinding 实例中查找相似的视图

Find similar views from DataBinding instance

我正在使用 Android DataBinding 并寻找一种方法从 ViewDataBinding 实例获取对 a/set 视图的引用。

我正在寻找一种通过唯一标识符查找视图的方法,在下面的代码中,是否可以在 [=37] 中获取具有相同标签 "MYTAG" 的所有视图=] 来自我的数据绑定实例(在 Activity,片段 class)?

android:tag="MYTAG"

我的实际用例是,我需要知道在 xml 中设置了 'android:transitionName' 属性的所有视图来制作场景过渡动画 (当开始新的开始时,我需要在 Bundle 选项中传递所有共享视图和 TransitionName activity 将要设置动画)

目前我正在使用 findViewById() 来获取所有感兴趣的视图,但我正在寻找一种方法来通过仅使用数据绑定来归档相同的视图,而无需通过其 ID 手动查找视图

这是我的 xml 布局:

`

<data>
    <variable name="item" type="demo.com.entities.Recommendation" />
</data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?android:attr/selectableItemBackground"
        >



        <ImageView
            android:id="@+id/room_card_icon"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:scaleType="fitXY"
            app:layout_constraintBottom_toTopOf="@+id/guide75"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/tour_placeholder"
            android:transitionName="@{item.uid}"
            app:imageUrl="@{item.image}"
            android:tag="MYTAG"
            />

        <TextView
            android:id="@+id/title_recommendations"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="@+id/room_card_icon"
            app:layout_constraintStart_toStartOf="parent"
            android:transitionName="@{`title`+item.uid}"
            android:text="@{item.poi.name}"
            tools:text="The Southern Ridges"
            android:tag="MYTAG"
             />

        <TextView
            android:id="@+id/typeTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toTopOf="@+id/title_recommendations"
            app:layout_constraintStart_toStartOf="@+id/title_recommendations"
            android:transitionName="@{`Category`+item.uid}"
            android:text="@{item.poi.dataset}"
            tools:text="Attractions"
            android:tag="MYTAG" />

        <TextView
            android:id="@+id/descriptionTV"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/guide75"
            android:text="@{item.recommendation}"
            tools:text="A magical place in nature. Take a morning walk or go for a sunset walk" />

           <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guide75"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.75" />

    </androidx.constraintlayout.widget.ConstraintLayout>

`

提前致谢!

我的问题变成了一个愚蠢的问题!在我读完后 this 我应该简单地解决这个问题。

无论如何回答我自己的问题:

我应该从绑定实例(“mBinding.getRoot()”)获取根视图,然后可以遍历子视图以通过几乎使用来查找视图任何东西,按任何属性(例如:按标签或按 transitionName 等)。

在我的例子中,我可以直接获取所有设置了 'transitionName' 名称的子视图。

   private fun getAllViewsWithTransitionName(root: ViewGroup): List<View> {
        val views = mutableListOf<View>()
        val childCount = root.childCount
        for (i in 0 until childCount) {
            val child = root.getChildAt(i)
            if (child is ViewGroup) {
                views.addAll(getAllViewsWithTransitionName(child))
            }

            val tagObj = child.transitionName
            if (!tagObj.isNullOrBlank()) {
                views.add(child)
            }

        }
        return views
    }

现在可以调用它来获取所有具有 TransitionName

的视图的列表
getAllViewsWithTransitionName(mBinding.root)

可以使用相同的方法来获取具有在 XML 中定义的相同标签的所有视图。 通过简单地替换这一行:

val tagObj = child.transitionName

val tagObj = child.tag

谢谢!!!!