简体   繁体   English

Android 数据绑定从 onClick 传递不同的视图

[英]Android Data Binding pass different view from onClick

I need to animate image2 clicking image1 using databinding.我需要使用数据绑定为 image2 单击 image1 设置动画。 I would like to know if exist a way to pass different view from onclick method in XML because I need image2 view.我想知道是否存在从 XML 中的 onclick 方法传递不同视图的方法,因为我需要 image2 视图。 In example below i pass view image1 but i would like to pass image2 view:在下面的示例中,我传递视图 image1,但我想传递 image2 视图:

 <?xml version="1.0" encoding="utf-8"?>
     <layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="MissingDefaultResource">

    <data>

        <import
            alias="v"
            type="android.view.View" />

        <variable
            name="viewModel"
            type="MainFragment" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/main_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/background_sky">


        <ImageButton
            android:id="@+id/image1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_margin="8dp"
            android:background="@drawable/ic_one"
            android:onClick="@{(v)-> viewModel.rotateImage(v)}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0" />

        <ImageButton
            android:id="@+id/image2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_margin="8dp"
            android:background="@drawable/ic_two"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="image1"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0" />

please try to modify the following code:请尝试修改以下代码:

  <ImageButton
            android:id="@+id/image1"
            ...
            android:onClick="@{(v)-> viewModel.rotateImage(image2)}"
            ... />

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

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