我很好奇如何使用 Android Jetpack 中的导航图从抽屉布局中的菜单项导航到嵌套导航图。我知道幕后有一些魔法可以将菜单项与基于 Id 的片段链接起来,但我不知道如何将菜单项链接到嵌套导航图。
例如,我使用 Android Studio 附带的默认导航抽屉活动项目。我已将 mobile_navigation.xml 修改为以下内容:
<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/mobile_navigation"
app:startDestination="@+id/nav_home">
<fragment
android:id="@+id/nav_home"
android:name="com.example.testdrawer.ui.home.HomeFragment"
android:label="@string/menu_home"
tools:layout="@layout/fragment_home" />
<include app:graph="@navigation/nested_navigation" />
<fragment
android:id="@+id/nav_tools"
android:name="com.example.testdrawer.ui.tools.ToolsFragment"
android:label="@string/menu_tools"
tools:layout="@layout/fragment_tools" />
<fragment
android:id="@+id/nav_share"
android:name="com.example.testdrawer.ui.share.ShareFragment"
android:label="@string/menu_share"
tools:layout="@layout/fragment_share" />
<fragment
android:id="@+id/nav_send"
android:name="com.example.testdrawer.ui.send.SendFragment"
android:label="@string/menu_send"
tools:layout="@layout/fragment_send" />
</navigation>
Run Code Online (Sandbox Code Playgroud)
我还添加了一个名为nested_navigation.xml 的新导航图,如下所示:
<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/nested_navigation"
app:startDestination="@+id/nav_gallery">
<fragment
android:id="@+id/nav_gallery"
android:name="com.example.testdrawer.ui.gallery.GalleryFragment"
android:label="@string/menu_gallery"
tools:layout="@layout/fragment_gallery" />
<fragment
android:id="@+id/nav_slideshow"
android:name="com.example.testdrawer.ui.slideshow.SlideshowFragment"
android:label="@string/menu_slideshow"
tools:layout="@layout/fragment_slideshow" />
</navigation>
Run Code Online (Sandbox Code Playgroud)
假设我的菜单如下所示:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_menu_camera"
android:title="@string/menu_home" />
<item …Run Code Online (Sandbox Code Playgroud) android android-navigation android-drawer android-jetpack android-jetpack-navigation
我目前正在尝试使用可绑定属性创建自定义元素,并将该属性绑定到repeat.for循环的变量.这似乎应该是一个简单的任务,但我不能让它工作,我想知道它是否可能与作为对象的变量有关.我的自定义元素的代码如下:
游戏card.js
import { bindable } from 'aurelia-framework';
export class GameCard {
@bindable gameData = null;
@bindable name = '';
bind() {
console.log('card-game-data: ' + JSON.stringify(this.gameData, null, 2));
console.log('name: ' + this.name);
}
}
Run Code Online (Sandbox Code Playgroud)
游戏card.html
<template>
<div class="card medium">
<h3 class="center-align left">${gameData.name}</h3>
<div class="right-align right">${gameData.isPublic}</div>
</div>
</template>
Run Code Online (Sandbox Code Playgroud)
使用自定义元素的视图如下:
<template>
<require from="./game-card"></require>
<div>
<div class="row">
<div repeat.for="game of games">
<game-card class="col s6 m4 l3" gameData.bind="game" name.bind="game.name"></game-card>
</div>
</div>
</div>
</template>
Run Code Online (Sandbox Code Playgroud)
游戏对象如下所示:
[{name: 'SomeName', isPublic: true}, {name: 'AnotherName', isPublic: false}]
Run Code Online (Sandbox Code Playgroud)
现在,当我运行代码时,game-card.js绑定方法中的console.log语句打印出未定义的gameData,但是为该console.log('name: …