gpt4 book ai didi

android - 是否可以在不在 Fragment 中编写样板代码的情况下将 androidx-navigation 与 onClick-databinding 一起使用?

转载 作者:行者123 更新时间:2023-11-29 23:10:44 25 4
gpt4 key购买 nike

我想实现一个 androidx-navigation通过使用数据绑定(bind)而不是在代码中实现 onClick 处理程序

我有一个带有实时数据元素 selectedItemId 的列表 fragment ,我想在按下编辑按钮时打开一个相应的详细 fragment 。有没有类似的可能

<layout>
<data>
<!-- assuming that com.example.MyGeneratedNavigation was
generated from .../res/navigation/my_navigation.xml -->
<variable name="myNavigation" type"com.example.MyGeneratedNavigation" />
<variable name="selectedItemId" type"..." />
</data>

<LinearLayout ...>
<Button android:id="@+id/edit"
android:onClick="@{() -> myNavigation.onShowDetails(selectedItemId)}" />

</LinearLayout>

</layout>

如果不在 List-fragment 中实现 onClick-handler 是否可行?

[@ABr 回答后更新]

建议的解决方案不会触发按钮事件。为了进一步调查,我创建了自己的静态函数来调试它是否被调用

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

<RelativeLayout ...>

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

</RelativeLayout>
</layout>

public class GalleryFragment extends Fragment {

public static final String TAG = "GalleryFragment";

public static void myNavigate(Object 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);
}

}

不幸的是,从未调用语句 Log.d

如果我在 java 代码中重命名静态方法 myNavigate 而不是在布局中,我会按预期得到编译错误,因此数据绑定(bind)在语法上是正确的。


我用这个导航图

<navigation 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"
android:id="@+id/nav_gallery"
app:startDestination="@id/galleryFragment">

<fragment
android:id="@+id/galleryFragment"
android:name="de.k3b.androidx.navigationdemo.GalleryFragment"
android:label="fragment_gallery"
tools:layout="@layout/fragment_gallery" >
<action
android:id="@+id/action_gallery_to_editProperties"
app:destination="@id/editPropertiesFragment" />
</fragment>
<fragment
android:id="@+id/editPropertiesFragment"
android:name="de.k3b.androidx.navigationdemo.EditPropertiesFragment"
android:label="fragment_edit_properties"
tools:layout="@layout/fragment_edit_properties" />
</navigation>

和这些build.gradle

全局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"

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
google()
jcenter()

}
}

应用构建.gradle

apply plugin: 'com.android.application'

android {
compileSdkVersion 28
defaultConfig {
applicationId "de.k3b.androidx.navigationdemo"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
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 fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation 'android.arch.navigation:navigation-fragment:1.0.0'
implementation 'android.arch.navigation:navigation-ui:1.0.0'
}

最佳答案

当然可以。首先,您应该在导航图中创建一个您计划移动的方向的连接。然后您可以使用下面的导入和 onclick。

请注意,我不确定您是否可以在此方法中传递参数!!!!!!

<layout>
<data>
<import type="com.rf.comm.R"/>
<import type="androidx.navigation.Navigation"/>
<!-- assuming that com.example.MyGeneratedNavigation was
generated from .../res/navigation/my_navigation.xml -->
<variable name="myNavigation" type"com.example.MyGeneratedNavigation" />
<variable name="selectedItemId" type"..." />
</data>

<LinearLayout ...>
<Button android:id="@+id/edit"
android:onClick="@{view -> Navigation.findNavController(view).navigate(R.id.createServiceFragment)}" />

</LinearLayout>

</layout>

关于android - 是否可以在不在 Fragment 中编写样板代码的情况下将 androidx-navigation 与 onClick-databinding 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56055891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com