如何将带有onClick的androidx数据绑定到带参数的静态方法 [英] Howto androidx-databinding for onClick to static method with parameter

查看:197
本文介绍了如何将带有onClick的androidx数据绑定到带参数的静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(注意:这是的后续问题,是否可以将androidx导航与onClick-databinding一起使用而无需在Fragment中编写锅炉代码?

我想使用androidx-databinding处理onClick调用带有参数的静态方法 Utils.myNavigate(...)通过xml-layout-file

I want to use androidx-databinding to handle onClick calling a static method with parameters Utils.myNavigate(...) via the xml-layout-file

下面的我的代码已通过编译过程验证 但是当我单击按钮时却从未被调用.

My Code below is verifyed by the compile process but is never called when i click on the button.

该如何解决?

我的布局文件:

<layout ...>
<data>
    <import type="de.k3b.androidx.navigationdemo.R" />
    <import type="de.k3b.androidx.navigationdemo.Utils" />
</data>

<RelativeLayout ...>

    <Button ...
        android:onClick="@{view -> Utils.myNavigate(view,
                                R.id.action_gallery_to_editProperties)}"
         />

</RelativeLayout>
</layout>

我的静态方法实施方式:

public class Utils {

public static final String TAG = "Utils";

public static void myNavigate(View view, @IdRes int id) {
    // this gets never called neither with `Object view` nor with `View view`
    Log.d (TAG, "myNavigate clicked");
    //  Navigation.findNavController(view).navigate(id);
}

}

全局项目build.gradle

buildscript {
repositories {
    google()
    jcenter()

}
dependencies {
    classpath 'com.android.tools.build:gradle:3.4.0'
    classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0"
}
}

allprojects {
repositories {
    google()
    jcenter()

}
}

app build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "de.k3b.androidx.navigationdemo"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }

        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    dataBinding.enabled=true

}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'android.arch.navigation:navigation-fragment:1.0.0'
    implementation 'android.arch.navigation:navigation-ui:1.0.0'
}

推荐答案

您可能忘记了将布局与活动绑定.

You might have forgot to bind the layout with the activity.

更改

setContentView(R.layout.your_layout_file);

收件人

DataBindingUtil.setContentView(this, R.layout.your_layout_file);

如果这不是问题,则可以检查工作示例使用数据绑定在调用静态方法时创建的.

If that's not the issue, you may check the working sample I created on calling static method using data binding.

这篇关于如何将带有onClick的androidx数据绑定到带参数的静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆