标签: android-navigation

如何将导航抽屉添加到现有代码中?

我有一个应用程序,其中我有一个带有菜单按钮的导航面板,我想在其上应用导航抽屉,目前占用整个屏幕,但我想通过在其上应用导航抽屉将其限制为屏幕的3/4 ,但我从来没有碰到导航抽屉之前,所以不知道,任何帮助或指导将great.Also通过一些文件去了,糊涂了:(这里是我的代码:

这是导航面板的布局类:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:foo="http://schemas.android.com/apk/res/com.justin.a"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_texture"
    android:clickable="true" >

<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="@dimen/nav_margin"
    android:layout_marginTop="@dimen/nav_margin"
    android:layout_marginRight="@dimen/nav_margin"
    android:layout_marginBottom="@dimen/nav_margin"
    android:background="#242424"
    >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="@dimen/nav_padding"
        android:paddingTop="@dimen/nav_padding"
        android:paddingRight="@dimen/nav_padding"

        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:divider="@android:drawable/divider_horizontal_bright"
            android:orientation="vertical"
            android:showDividers="middle"

             >


             <com.justin.a.utils.FontTextView
                android:id="@+id/nav_option_dashboard"
                style="@style/a.TextView.NavigationItem"
                android:layout_width="match_parent"
                android:layout_height="@dimen/button_ht"
                android:layout_marginBottom="1px"
                android:onClick="onDashboardClicked"
                android:text="@string/nav_option_dashboard"
                android:textSize="@dimen/navigation_panel_text"
                foo:customFont="cabin.medium.ttf"
                android:padding="@dimen/nav_option_padding"
                android:background="@drawable/nav_background_button"
                    />  
            <com.justin.a.utils.FontTextView
                android:id="@+id/nav_option_news"
                style="@style/a.TextView.NavigationItem"
                android:layout_width="match_parent"
                android:layout_height="@dimen/button_ht"
                foo:customFont="cabin.medium.ttf"
                android:textSize="@dimen/navigation_panel_text"
                android:layout_marginBottom="1px"
                android:onClick="onNewsClicked"
                android:text="@string/nav_option_news"
                android:padding="@dimen/nav_option_padding"
                android:background="@drawable/nav_background_button"

                 />

            <com.justin.a.utils.FontTextView
                android:id="@+id/nav_option_markets"
                style="@style/a.TextView.NavigationItem"
                android:layout_width="match_parent"
                android:layout_height="@dimen/button_ht"
                android:textSize="@dimen/navigation_panel_text"
                android:layout_marginBottom="1px"
                android:onClick="onMarketClicked"
                android:text="@string/nav_option_markets" 
                foo:customFont="cabin.medium.ttf"
                android:padding="@dimen/nav_option_padding"
                android:background="@drawable/nav_background_button"

                />

            <com.justin.a.utils.FontTextView
                android:id="@+id/nav_option_lists"
                style="@style/a.TextView.NavigationItem"
                android:layout_width="match_parent" …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-activity android-navigation

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

2 ViewPager在Android 2.3中不滚动

我在我的应用程序中使用导航抽屉,其中包含一些碎片,如下图所示.

在此输入图像描述

每个片段包含在另一个ViewPager这是一个ImageSlider,再下面是一个ListView和顶部我使用的SwipeRefreshLayout.我的问题是图像滑块在Android 3.0或更高版本的设备上运行良好,但向左或向右滑动不适用于设备2.3及更低版本,而是调用父ViewPager的滑动,即导航片段.我正在使用支持版本4库来支持低于3.0的设备.除了那一个,所有功能在2.3设备上运行良好.我用谷歌搜索了它,但我在任何地方都没有找到任何帮助.因此,为了让它滚动我应该为此做什么,任何想法/帮助将受到高度赞赏.

android android-fragments android-2.3-gingerbread android-viewpager android-navigation

9
推荐指数
1
解决办法
2456
查看次数

Jetpack导航:NavHostManager不是FragmentManager的活动片段

我正在使用Jetpack Navigation来处理Fragments的导航.
我一直在关注文档并安装了所需的组件,当尝试显示托管NavHost片段的活动时,应用程序仍然崩溃

例外:

java.lang.IllegalArgumentException: Fragment NavHostFragment{820022f} is not an active fragment of FragmentManager FragmentManager{5a5703c in HostCallbacks{a0b41c5}}
        at android.support.v4.app.FragmentManagerImpl.setPrimaryNavigationFragment(FragmentManager.java:3389)
        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:783)
        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2595)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2382)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2337)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2244)
Run Code Online (Sandbox Code Playgroud)

主要活动布局

<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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LoginActivity" >

    <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"
        />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

主要活动 - Kotlin

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.login_activity)
        if (savedInstanceState == null) {
            supportFragmentManager.beginTransaction()
                    .replace(R.id.container, LoginFragment.newInstance())
                    .commitNow()
        }
    }

    override fun onSupportNavigateUp()
        = findNavController(R.id.my_nav_host_fragment).navigateUp()

} …
Run Code Online (Sandbox Code Playgroud)

android android-navigation kotlin-android-extensions android-jetpack android-architecture-navigation

9
推荐指数
1
解决办法
2793
查看次数

导航组件弹出行为无法正常工作

在我正在构建的应用程序中,我使用了单一活动架构,并决定使用 Google 的新导航组件来浏览应用程序。
虽然它显示出很大的希望,但它有一些缺点,我的问题是关于其中之一。

假设我们有三个按顺序导航的片段,除了当我们在第三个片段上单击后退按钮时我们想返回到第一个片段。这是它的过程:

从一到二再到三的导航

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_nav_graph.xml"
    app:startDestination="@id/firstFragment">

    <fragment
        android:id="@+id/firstFragment"
        android:name="com.hmomeni.navisample.FirstFragment"
        android:label="fragment_first"
        tools:layout="@layout/fragment_first" >
        <action
            android:id="@+id/action_firstFragment_to_secondFragment"
            app:destination="@id/secondFragment" />
    </fragment>
    <fragment
        android:id="@+id/secondFragment"
        android:name="com.hmomeni.navisample.SecondFragment"
        android:label="fragment_second"
        tools:layout="@layout/fragment_second" >
        <action
            android:id="@+id/action_secondFragment_to_thirdFragment"
            app:destination="@id/thirdFragment"
            app:popUpTo="@+id/firstFragment" />
    </fragment>
    <fragment
        android:id="@+id/thirdFragment"
        android:name="com.hmomeni.navisample.ThirdFragment"
        android:label="fragment_third"
        tools:layout="@layout/fragment_third" />
</navigation>
Run Code Online (Sandbox Code Playgroud)

这里的问题是,当我想第二次重复导航时,会发生异常告诉我:

java.lang.IllegalArgumentException: 导航目的地 com.hmomeni.navisample:id/action_firstFragment_to_secondFragment 对这个 NavController 是未知的

进一步调查显示,在点击返回按钮并返回到第一个片段时,navController.currentDestination仍然指的是ThirdFragment哪个是错误的,应该是FirstFragment

对此的任何帮助表示赞赏。

android android-navigation android-jetpack android-architecture-navigation

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

Android Jetpack使用BottomNavigationView导航正确的后台堆栈

Android的程式码实验室用于导航做得很好,说明如何用一起使用的架构组件导航BottomNavigationView.但是,假设我在2个标签BottomNavigationView,TAB1TAB2.让我们说在Tab1中你可以浏览片段Frag1 - > Frag2.现在,每当我去TAB2,然后回到TAB1,片段回栈Frag1,2走了,并与起点更换Frag1一次.

我在应用程序中需要做什么,以便即使我更改标签,BottomNavigationView导航一起保持其后堆栈完好无损?并且,还保持后退/上行按钮行为与指南同步.

以前我通过使用ViewPager和管理后台堆栈来完成这项任务,但这并不像使用新导航的正确方法.

提前致谢!


编辑:

还有一个更详尽的答案在这里.

android android-navigation bottomnavigationview android-architecture-components android-jetpack

9
推荐指数
1
解决办法
2164
查看次数

popUpTo 似乎在导航组件中不起作用

所以我使用的是 android 导航组件,但我遇到了问题(2.2.0-rc04 版本)。

我有一个welcomeFragment(wF)。从wF我想导航到loginSellerFragment不同导航图中的 (lSF)。我也不想wF在导航到时从 backstack (popUpTo, popUpToInclusive) 中删除,lSF因为用户可能想回到它。

<fragment
    android:id="@+id/welcomeFragment">
    <action
        android:id="@+id/action_welcomeFragment_to_nav_onboarding_seller"
        app:launchSingleTop="true"
        app:destination="@id/nav_onboarding_seller" />
</fragment>
Run Code Online (Sandbox Code Playgroud)

导航到 lSF 后,backstack 如下所示:wF lSF

我们lSF现在开始,登录后我们想转到feedFragment(fF) ,它再次位于单独的图表中,但这次我们想清除所有后台堆栈,因为如果用户登录并按回,他希望应用程序退出,不是为了带他回到wFor lSF,所以我popUpTo="@id/loginSellerFragment popUpToInclusive='true"在动作中使用了lSFto fF

<fragment
    android:id="@+id/loginSellerFragment">
    <action
        android:id="@+id/action_login_to_seller"
        app:destination="@+id/seller" . //this is the graph that has as firstDestination, feedFragment
        app:launchSingleTop="true"
        app:popUpTo="@id/loginSellerFragment"
        app:popUpToInclusive="true" />
</fragment>
Run Code Online (Sandbox Code Playgroud)

所以在这个时刻的 backstack 应该只有fF因为我们删除了所有的东西lSFlSF包括) …

android android-fragments android-navigation

9
推荐指数
2
解决办法
4507
查看次数

为什么Android Navigation Component 屏幕不返回上一个Fragment,而是调用了previos Fragment 的onViewCreated 中的一个方法?

我有2个片段通话CreateRoomFragmentDisplayPhotoFragment,导航图是这个样子的:

<navigation>    
<fragment
    android:id="@+id/createRoomFragment"
    android:name="package.room.CreateRoomFragment"
    android:label="Create a room"
    tools:layout="@layout/fragment_create_room">
    <action
        android:id="@+id/action_createRoomFragment_to_roomFragment"
        app:destination="@id/roomFragment" />
    <action
        android:id="@+id/action_createRoomFragment_to_displayPhotoFragment"
        app:destination="@id/displayPhotoFragment" />
</fragment>

<fragment
    android:id="@+id/displayPhotoFragment"
    android:name="package.fragment.DisplayPhotoFragment"
    android:label="fragment_display_photo"
    tools:layout="@layout/fragment_display_photo" >
    <argument android:name="bitmap"
              app:argType="android.graphics.Bitmap"/>
</fragment>
Run Code Online (Sandbox Code Playgroud)

所以当我想从 to 移动CreateRoomFragmentDisplayPhotoFragment,我使用如下 do:

NavDirections action = CreateRoomFragmentDirections.actionCreateRoomFragmentToDisplayPhotoFragment(selectedPhoto);
Navigation.findNavController(view).navigate(action);
Run Code Online (Sandbox Code Playgroud)

这样做,我可以导航到DisplayPhotoFragment.

但是当我按下back设备的按钮以及Back arrow工具栏中的 时,它无法返回到CreateRoomFragment

我试过这个,但仍然无法回到上一个片段:

requireActivity().getOnBackPressedDispatcher().addCallback(getViewLifecycleOwner(),
                new OnBackPressedCallback(true) {
                    @Override
                    public void handleOnBackPressed() {

                       navController.navigateUp();  //I tried this 
                       navController.popBackStack(R.id.createRoomFragment,false); //and also this
                    }
                });
Run Code Online (Sandbox Code Playgroud)

现在主要问题:

使用上面的代码,屏幕没有回到之前的Fragment( …

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

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

Android Navigation Component:如何解决嵌套图的循环导航?

在我们的用户需求中,我们有这样的流程。

在此处输入图片说明

我搜索了以下内容:

https://developer.android.com/guide/navigation/navigation-principles

嵌套导航图的循环参考

但没有什么可以帮助...

我做了什么?

目前我拆分了 2 个图并在 Graph2 中添加了重复的片段 C。此外,我以编程方式将图开始目标更改为图 1 中的片段 C,只有我从图 2 中单击。这是一个丑陋的解决方案但我不知道如何解决这个问题。

我的问题是:有什么办法可以做到而不是制作重复的目的地并更改起始目的地?

图一:

<include app:graph="@navigation/graph2" />
<dialog
        android:id="@+id/fragmentA"
        android:label="Fragment A"
        tools:layout="@layout/fragmentA"/>
<dialog
        android:id="@+id/fragmentB"
        android:label="Fragment B"
        tools:layout="@layout/fragmentB">
    <action
            android:id="@+id/NavigateToGraph2"
            app:destination="@id/graph2">
</dialog>
<dialog
        android:id="@+id/fragmentC"
        android:label="Fragment C"
        tools:layout="@layout/fragmentC"/>
<dialog
        android:id="@+id/fragmentD"
        android:label="Fragment D"
        tools:layout="@layout/fragmentD"/>
<dialog
        android:id="@+id/fragmentE"
        android:label="Fragment E"
        tools:layout="@layout/fragmentE"/>
Run Code Online (Sandbox Code Playgroud)

图2

<dialog
        android:id="@+id/fragmentF"
        android:label="Fragment F"
        tools:layout="@layout/fragmentF"/>
<dialog
        android:id="@+id/fragmentG"
        android:label="Fragment G"
        tools:layout="@layout/fragmentG"/>
<dialog
        android:id="@+id/fragmentC"
        android:label="Fragment C"
        tools:layout="@layout/fragmentC"/>
Run Code Online (Sandbox Code Playgroud)

android android-navigation android-architecture-components android-architecture-navigation

9
推荐指数
1
解决办法
428
查看次数

底部导航视图 OnNavigationItemSelectedListener 已弃用

我正在按照Material Design尝试 Android 的 BottomNavigationView 实现

但是,在 MainActivity 代码上,我收到一条OnNavigationItemSelectedListener已弃用的警告- 请参阅下面的快照

在此处输入图片说明

尝试使用另一种方法来处理 BottomNavigationView,但我找不到它。

寻求任何有出路的人的帮助,但与此同时,我已将我的 BottomView 的菜单项 ID 与片段目标 ID 进行了匹配,并且我成功实现了导航,但存在无法使用片段名称更新我的工具栏标题的限制。

android android-navigation

9
推荐指数
3
解决办法
2587
查看次数

使用导航组件,使用嵌套在其他 NavHostFragments 中的 NavHostFragments 时向上导航的正确方法是什么

请注意,我在这里不是在谈论嵌套导航图。我特别谈论使用NavHostFragments作为 other 的子部分NavHostFragments并使用 NavigationComponent 在子片段和父片段之间来回转换。话虽如此...

我们如何在其他 NavHostFragments 中正确使用带有 NavHostFragments 的导航组件?

例如:假设我有MainActivity以下布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            style="@style/Toolbar"/>
    </android.support.design.widget.AppBarLayout>
    <fragment
        android:id="@+id/navHost"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

Main Activity 有一个NavHostFragmentidnavHost使用导航图nav_graph,如下所示:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_graph"
    app:startDestination="@id/fragmentA">
    <fragment
        android:id="@+id/fragmentA"
        android:name="com.example.nestednavfragmentstest.FragmentA"
        android:label="FragmentA"
        tools:layout="@layout/fragment_a">
        <action android:id="@+id/action_fragmentA_to_fragmentB" app:destination="@id/fragmentB"/>
    </fragment>
    <fragment
        android:id="@+id/fragmentB"
        android:name="com.example.nestednavfragmentstest.FragmentB"
        android:label="FragmentB"
        tools:layout="@layout/fragment_b"/>
</navigation>
Run Code Online (Sandbox Code Playgroud)

本质上, …

android android-navigation android-architecture-navigation

8
推荐指数
0
解决办法
2443
查看次数