【问题标题】:Find similar views from DataBinding instance从 DataBinding 实例中查找类似视图
【发布时间】:2019-02-18 09:33:27
【问题描述】:

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

`

提前谢谢你!

【问题讨论】:

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


    【解决方案1】:

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

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

    我应该从绑定实例(“mBinding.getRoot()”)中获取根视图,然后可以通过几乎使用任何属性(例如: 通过 Tag 或 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

    谢谢!!!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-04
      • 2013-07-19
      相关资源
      最近更新 更多