【发布时间】: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