简体   繁体   English

绑定适配器中的多个视图

[英]multiple views in binding adapter

I have a button.我有一个按钮。 When the button is clicked, the button and a textView are animated.当按钮被点击时,按钮和一个 textView 被动画化。 The question is: how to get multiple views on the binding adapter?问题是:如何在绑定适配器上获得多个视图? Is the way I did it correct?我做的方法正确吗?

<variable
    name="variableTextViewDescription"
    type="androidx.appcompat.widget.AppCompatTextView" />
fun bind(task: Task, viewModel: ToDoListViewModel) {
            binding.task = task
            binding.viewModel = viewModel
            binding.variableTextViewDescription = binding.textViewDescription
            binding.executePendingBindings()
        }
@BindingAdapter(value = ["task", "textViewDescription"], requireAll = true)
fun ImageButton.setOnClickButtonMore(task: Task, textViewDescription: AppCompatTextView) {

    if (task.isExpanded) {
        toggleArrow(this, false, textViewDescription)
    } else {
        toggleArrow(this, true, textViewDescription)
    }

    this.setOnClickListener {
        task.isExpanded = toggleArrow(it, task.isExpanded, textViewDescription)
    }

}
<ImageButton
                    android:id="@+id/buttonMore"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:task="@{task}"
                    app:textViewDescription="@{variableTextViewDescription}"
                    android:background="?attr/selectableItemBackgroundBorderless"
                    android:src="@drawable/ic_baseline_keyboard_arrow_down_24"
                    tools:ignore="ContentDescription" />

I can propose a solution for you, that maybe different from adding multiple Views to the same Binding Adapter.我可以为您提出一个解决方案,这可能与将多个视图添加到同一个绑定适配器不同。

You can add a MutableLiveData when changed by Button click, it starts the animation.您可以在通过单击按钮更改时添加MutableLiveData ,它会启动动画。

So, we will have a single MutableLiveData added to 2 Binding Adapters (the button binding adapter and the ImageView binding adapter).因此,我们将一个 MutableLiveData 添加到2 个绑定适配器(按钮绑定适配器和 ImageView 绑定适配器)。

when the value of the MutableLiveData changed, both binding adapters will fire and in both adapters load your animation.当 MutableLiveData 的值发生变化时,两个绑定适配器都会触发并在两个适配器中加载您的动画。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM