I want to implement an androidx-navigation by using databinding instead of implementing onClick
handler in code
I have a List-fragment with a livedata element selectedItemId
and want to open a correspondeng detail-fragment when pressing the edit button. Is something similar to this possible
<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>
Is this possible without implementing a onClick-handler in the List-fragment ?
[Update after @ABr answer]
The proposed solution does not trigger the button event. For further investigations i created my own static function to debug if it gets called
<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);
}
}
Unfortulately the statement Log.d
is never called.
If i rename the static method myNavigate
in the java code and not in layout i get as expected a compile error so databinding is syntactically ok.
i use this nav graph
<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>
and these build.gradle
global 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()
}
}
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"
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'
}