【问题标题】:multiple views in binding adapter绑定适配器中的多个视图
【发布时间】:2021-09-09 13:52:19
【问题描述】:

我有一个按钮。单击按钮时,按钮和 textView 会被动画化。问题是:如何在绑定适配器上获得多个视图?我的做法对吗?

<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" />

【问题讨论】:

    标签: android android-databinding


    【解决方案1】:

    我可以为您提出一个解决方案,这可能不同于将多个视图添加到同一个绑定适配器。

    您可以添加一个 MutableLiveData,当通过 Button 点击​​更改时,它会启动动画。

    因此,我们将一个 MutableLiveData 添加到 2 个绑定适配器(按钮绑定适配器和 ImageView 绑定适配器)。

    当 MutableLiveData 的值发生变化时,两个绑定适配器将触发,并在两个适配器中加载您的动画。

    【讨论】:

    • 我制作了两个采用布尔值的适配器,但我不知道如何在这种情况下正确实现 MutableLiveData。你能给我举个例子吗?
    猜你喜欢
    • 2020-06-14
    • 2020-06-01
    • 2017-06-06
    • 1970-01-01
    • 2019-01-28
    • 1970-01-01
    • 1970-01-01
    • 2011-02-05
    • 1970-01-01
    相关资源
    最近更新 更多