我实际上在我的应用程序中使用了新的Architecture组件,并且已经设置了导航组件。我有一个导航抽屉,我想和它一起使用。我已经设置好了,但是遇到了一些问题:
1-抽屉不会自行关闭。菜单可以正常工作并导航到正确的位置,但导航后不会将其关闭。我必须添加一个destinationChangedListener自己关闭它。
navController.addOnDestinationChangedListener { _, _, _ ->
if (drawer_layout.isDrawerOpen(GravityCompat.START))
drawer_layout.closeDrawer(GravityCompat.START)
}
在codelabs中,抽屉会自行关闭,我真的不明白为什么。
2-向上按钮打开抽屉。当我导航到非顶层片段时,菜单图标变为向上箭头,但是当我单击它时,它将打开抽屉菜单,而不是返回到先前的目的地。
3对于菜单的某些项目,我想在点击时设置自定义。菜单上的所有按钮均不用于导航至应用程序,而且我不知道在哪里可以覆盖navController的行为以告知其在无法导航时该怎么做。
以下是相关代码:
ActivityMain.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/layout_app_bar"/>
<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_marginTop="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/app_navigation" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<include layout="@layout/layout_navigation_view"/>
</androidx.drawerlayout.widget.DrawerLayout>
</FrameLayout>
Layout_app_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:theme="@style/AppTheme.AppBarOverlay"
xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/language_main_color"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
drawer_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="@+id/appAction" >
<item
android:id="@+id/bridgeFragment"
android:icon="@drawable/ic_home"
android:title="@string/home" />
<item
android:id="@+id/categoryFragment"
android:icon="@drawable/ic_categories"
android:title="@string/categories" />
<item
android:id="@+id/favoriteFragment"
android:icon="@drawable/ic_favorite"
android:title="@string/favorites" />
<item
android:id="@+id/unlimited"
android:icon="@drawable/ic_unlimited"
android:title="@string/dialog_ask_unlimited_title" />
<!--<item
android:id="@+id/settings"
android:icon="@drawable/ic_settings"
android:title="@string/settings" /> -->
</group>
<group android:id="@+id/globalAction">
<item
android:id="@+id/rate"
android:icon="@drawable/ic_rate"
android:title="@string/rate" />
<item
android:id="@+id/aboutFragment"
android:icon="@drawable/ic_about_me"
android:title="@string/about"/>
</group>
</menu>
MainActivity.kt(在onCreate中调用setupMenus()和setupView())
override fun setupView(){
val host: NavHostFragment = supportFragmentManager
.findFragmentById(R.id.nav_host_fragment) as NavHostFragment? ?: return
val navController = host.navController
val drawerLayout : DrawerLayout? = findViewById(R.id.drawer_layout)
appBarConfiguration = AppBarConfiguration(
setOf(R.id.bridgeFragment, R.id.categoryFragment, R.id.favoriteFragment, R.id.aboutFragment),
drawerLayout)
setupActionBarWithNavController(navController, appBarConfiguration)
setupNavigationMenu(navController)
navController.addOnDestinationChangedListener { _, _, _ ->
if (drawer_layout.isDrawerOpen(GravityCompat.START))
drawer_layout.closeDrawer(GravityCompat.START)
}
}
fun updateHasMenu(hasMenu: Boolean) { this.hasMenu = hasMenu }
private fun setupNavigationMenu(navController: NavController) {
val sideNavView = findViewById<NavigationView>(R.id.nav_view)
sideNavView?.setupWithNavController(navController)
}
override fun onSupportNavigateUp(): Boolean {
return findNavController(R.id.nav_host_fragment).navigateUp(appBarConfiguration)
}
override fun setupMenus(){
setSupportActionBar(toolbar)
supportActionBar?.let {
it.setDisplayHomeAsUpEnabled(true)
it.setHomeAsUpIndicator(R.drawable.ic_menu)
}
}
答案 0 :(得分:0)
这是第一个,第二个和第三个问题的全部工作代码。
library(ANTsR)
# load example image and make a mask
img = antsImageRead(getANTsRData( "r16" ))
mask = getMask(img)
###########################################
library(doParallel)
ncores = 2
registerDoParallel(cores=ncores)
###############################
# parallel loop cannot update img S4 object
foreach (i=1:10, .combine='c', .export = c('img','mask'),
.packages = c('ITKR', 'ANTsRCore', 'ANTsR') ) %dopar% {
# max(img) # output: 254
# print(img@pointer) # output: <pointer: (nil)>
img[mask] = 0 # output: no change to img
}
# standard for loop works
for (i in 1:10) {
# print(img@pointer) output: <pointer: 0x5510610>
# max(img) # output: 254
img[mask] = 0 # output: img black
}
}