如何将 onClick 的 androidx 数据绑定到带参数的静态方法
Howto androidx-databinding for onClick to static method with parameter
(注意:这是
的后续问题
我想使用 androidx-databinding 来处理 onClick
调用带有参数的静态方法
Utils.myNavigate(...)
通过 xml-布局文件
我下面的代码已经过编译验证
但是当我点击按钮时从未被调用。
如何解决这个问题?
我的布局文件:
<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()
}
}
应用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'
}
您可能忘记了将布局与 activity 绑定。
改变
setContentView(R.layout.your_layout_file);
到
DataBindingUtil.setContentView(this, R.layout.your_layout_file);
如果这不是问题,您可以查看我在使用数据绑定调用静态方法时创建的 working sample。
(注意:这是
我想使用 androidx-databinding 来处理 onClick
调用带有参数的静态方法
Utils.myNavigate(...)
通过 xml-布局文件
我下面的代码已经过编译验证 但是当我点击按钮时从未被调用。
如何解决这个问题?
我的布局文件:
<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()
}
}
应用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'
}
您可能忘记了将布局与 activity 绑定。
改变
setContentView(R.layout.your_layout_file);
到
DataBindingUtil.setContentView(this, R.layout.your_layout_file);
如果这不是问题,您可以查看我在使用数据绑定调用静态方法时创建的 working sample。