标签: android-navigation

如何在一定的延迟时间后隐藏 ActionBar 和 NavigationBar?

几秒钟后,我会隐藏我的应用程序的导航栏和操作栏,显示不会被触摸,并将当前视图扩展到全屏。然后,如果用户触摸屏幕(或者向下滑动更好),则再次使两者可见。如何?

android delay android-actionbar android-navigation

6
推荐指数
1
解决办法
3157
查看次数

Android 导航架构在手机屏幕关闭时无法导航

我的项目中有以下架构:

MainActivity 布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/my_nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        app:navGraph="@navigation/nav_graph"
        app:defaultNavHost="true"
        />

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

nav_graph 设计:

在此处输入图片说明

nav_graph xml:

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/nav_graph"
    app:startDestination="@id/loginFragment">

    <fragment
        android:id="@+id/loginFragment"
        android:name="com.example.LoginFragment"
        android:label="LoginFragment" >
        <action
            android:id="@+id/loginToContentAction"
            app:destination="@id/contentFragment" />
    </fragment>
    <fragment
        android:id="@+id/contentFragment"
        android:name="com.example.ContentFragment"
        android:label="ContentFragment" />
</navigation>
Run Code Online (Sandbox Code Playgroud)

在 中LoginFragment,我有以下逻辑:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    if(presenter.isUserLogged()) {
        // getNav() returns NavController from the MainActivity
        getNav().navigateTo(R.id.loginToContentAction)
        return
    }

    // init login views etc …
Run Code Online (Sandbox Code Playgroud)

android kotlin android-navigation android-architecture-navigation

6
推荐指数
1
解决办法
269
查看次数

如何返回导航图的根?

如何使用 返回到根 Fragment NavController?我试过

navController.popBackStack(navController.graph.startDestination, false)
Run Code Online (Sandbox Code Playgroud)

但它没有用。这确实有效:

navController.navigate(navController.graph.startDestination, false)
navController.popBackStack(0, false)
Run Code Online (Sandbox Code Playgroud)

但我想知道这是否是解决这个问题的正确方法。

android android-navigation android-architecture-navigation

6
推荐指数
1
解决办法
4229
查看次数

Android 导航组件。如何在不创建新图的情况下覆盖另一个构建变体的片段或操作

我在 gradle 中有 10 种风格(构建变体)和大约 50 个片段作为屏幕。结果 nav_graph 非常大(~700 行)。扩展一些片段是一个微不足道的问题,我对每个构建变体都有 10% 的扩展

如何仅覆盖另一个构建变体的片段或操作以避免重复?

android android-navigation android-jetpack

6
推荐指数
0
解决办法
543
查看次数

如何清除导航组件中的碎片堆栈?

我像这样尝试使用 FragmentManager:

Navigation.findNavController(mView).popBackStack()
Run Code Online (Sandbox Code Playgroud)

并尝试使用FragmentManager.

val fm = activity!!.supportFragmentManager
Run Code Online (Sandbox Code Playgroud)

1.

fm.popBackStack()
Run Code Online (Sandbox Code Playgroud)

2.

fm.popBackStackImmediate()
Run Code Online (Sandbox Code Playgroud)

但他们没有工作。我的意思是,它们似乎弹出所有的片段。

我还尝试将这些属性应用于graph_nav.xml

app:launchSingleTop="true"
app:popUpToInclusive="true"
Run Code Online (Sandbox Code Playgroud)

我将它们应用于 A 片段和 B 片段。

我想从A片段转到B片段。我想清除 B 片段之前的所有片段堆栈,只留下 B 片段。

android android-fragments android-navigation

6
推荐指数
1
解决办法
1万
查看次数

无法找到 DialogFragment 中的 NavController

我有一个包含 NavHostFragment 的 BottomSheetDialogFragment,如下所示:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <fragment
                android:id="@+id/add_feeling_nav_host_fragment"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                app:defaultNavHost="true"
                app:navGraph="@navigation/add_feeling_graph" />

    </LinearLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)

当我尝试使用以下任一方法在其类中获取 NavController 时:

activity?.findNavController(R.id.add_feeling_nav_host_fragment)
view.findNavController()
Run Code Online (Sandbox Code Playgroud)

第一个选项崩溃,因为它找不到 ID,而第二个选项确实找到了父导航控制器并使用它。

我为另一个 Fragment 设置了相同的设置,但它不是一个对话框,而且运行良好。有什么想法可能是什么问题?谢谢

android android-dialogfragment android-navigation android-architecture-navigation

6
推荐指数
1
解决办法
1452
查看次数

Android 导航弹出动画不起作用

我目前正在尝试使用 Android 导航组件从一个片段 (A) 导航到另一个 (B),方法是从屏幕底部(在片段 A 上)向上动画片段 B,然后在按下后退按钮时将片段 B 向下动画被按下。我目前为此定义了一个操作:

<action
        android:id="@+id/action_landingFragment_to_bookingFragment"
        app:destination="@id/bookingFragment"
        app:enterAnim="@anim/booking_screen_in"
        app:exitAnim="@anim/nothing"
        app:popEnterAnim="@anim/nothing"
        app:popExitAnim="@anim/booking_screen_out"
        app:popUpTo="@id/landingFragment" />
Run Code Online (Sandbox Code Playgroud)

然而,当我当前从片段 A 导航到片段 B 时,我从屏幕底部平滑地向上滑动,点击后退按钮并看到片段 B 立即消失以再次显示片段 A,没有动画。我目前没有为片段 B 上的后退按钮使用自定义代码(尽管我尝试navigateUp()popBackStack()在 anOnBackPressedCallback中查看我是否遗漏了有关这些的内容。我还想知道片段的 Z 轴是否存在问题(类似于此线程) ) 但我的正常进入/退出动画工作正常,我尝试了一组更传统的动画(向左/向右滑动),其中进入和退出动画播放,但弹出动画再次不播放。

任何人都可以提出正确的返回导航方法,以便这些流行动画应该可见吗?

android android-fragments android-navigation android-architecture-navigation

6
推荐指数
0
解决办法
388
查看次数

Android Jetpack 导航 - 如何从抽屉菜单项导航到嵌套导航图

我很好奇如何使用 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

6
推荐指数
1
解决办法
714
查看次数

找不到 android.arch.navigation:navigation-safe-args-gradle-plugin:2.3.5。?

我正在使用 android 导航组件,但出现错误配置根项目“AndroidCodingChallenge”时出现问题。

无法解析配置“:classpath”的所有工件。找不到 android.arch.navigation:navigation-safe-args-gradle-plugin:2.3.5。在以下位置进行了搜索:

在我的 app.gradle 下面

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}
apply plugin: 'androidx.navigation.safeargs.kotlin'
apply plugin: 'kotlin-kapt'
apply from: '../commons.gradle'

android {
    compileSdkVersion 30
    buildFeatures {

        dataBinding = true

        // for view binding:
        // viewBinding = true
    }

    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.example.androidcodingchallenge"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables{
            useSupportLibrary = true
        }

        multiDexEnabled true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility …
Run Code Online (Sandbox Code Playgroud)

android gradle kotlin android-navigation android-safe-args

6
推荐指数
1
解决办法
6099
查看次数

Jetpack Compose:使用导航组件更改底部导航后按行为

我终于开始使用 Android 版 Compose,但目前陷入导航困境。我有两个问题:

1. 我实现了带有三个按钮的底部导航,其中一个按钮触发嵌套导航图,例如:

 HOME  ---  CONTACTS  ---  FAVOURITES 
   |            
DETAILS 
Run Code Online (Sandbox Code Playgroud)

因此,当我从主页打开详细信息屏幕并切换选项卡时,行为是正确的,嵌套图中的状态和最后目的地会被记住,当我再次点击主页时,它会保留状态并切换回详细信息。但是,如果我在任何其他选项卡上并按返回,它会将我带回到主页,但会重置状态,不会让我返回详细信息。我怎样才能做到这一点?

导航栏:

@Composable
fun BottomNavigationBar(navController: NavHostController) {
BottomNavigation {
    val backStackEntry by navController.currentBackStackEntryAsState()
    val currentRoute = backStackEntry?.destination?.route

    BottomNavigationCollection.items.forEach { navItem ->
        BottomNavigationItem(
            selected = currentRoute == navItem.route,
            onClick = {
                navController.navigate(navItem.route) {
                    popUpTo(navController.graph.findStartDestination().id) {
                        saveState = true
                    }
                    launchSingleTop = true
                    restoreState = true
                }
            },
            icon = {
                Icon(
                    imageVector = navItem.icon,
                    contentDescription = navItem.title
                )
            },
            label = {
                Text(text = navItem.title)
            },
        )
    } …
Run Code Online (Sandbox Code Playgroud)

android kotlin android-navigation android-jetpack-compose

6
推荐指数
1
解决办法
2263
查看次数