databinding一起使用

databinding一起使用

本文介绍了是否可以将androidx导航与onClick-databinding一起使用,而无需在Fragment中编写锅炉代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过使用数据绑定而不是实现 androidx导航 >代码中的处理程序

I want to implement an androidx-navigation by using databinding instead of implementing onClick handler in code

我有一个带有livedata元素selectedItemId的列表片段,并且想在按下编辑按钮时打开一个对应的细节片段.有可能与此类似吗

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>

是否可以在列表片段中不实现onClick-handler呢?

Is this possible without implementing a onClick-handler in the List-fragment ?

[@ ABr回答后更新]

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

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);
}

}

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

Unfortulately the statement Log.d is never called.

如果我在Java代码中而不是在布局中重命名静态方法myNavigate,则会出现编译错误,因此从语法上讲数据绑定是可以的.

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.

我使用此导航图

<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

and these build.gradle

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

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'
}

推荐答案

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

Yes of course you can. Firstly you should create a connection in the navgraph to which direction you are planning to move.Then you can use the below imports and onclick.

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

Please note I am not sure if you can pass parameters inside this method!!!!!

<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>

这篇关于是否可以将androidx导航与onClick-databinding一起使用,而无需在Fragment中编写锅炉代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 21:55