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

时间:2019-02-18 09:33:27

标签: android data-binding kotlin android-recyclerview android-databinding

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

我正在寻找一种通过唯一标识符查找视图的方法,在下面的这段代码中,是否有可能从我的数据绑定实例中获取xml中具有相同标签“ MYTAG”的所有视图(在Activity中,Fragment类)?

  

android:tag =“ MYTAG”

我的实际用例是,我需要了解所有在xml中设置了'android:transitionName'属性的视图,以制作场景过渡动画 (在要进行动画的新启动活动时,我需要在Bundle选项中传递所有共享的视图和TransitionName)

目前,我正在使用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>

`

提前谢谢!

1 个答案:

答案 0 :(得分:0)

我的问题最终变成一个愚蠢的问题!我读完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

谢谢!!!!

相关问题