标签: android-navigation

通过活动实现的Android导航抽屉

我正在开发自己的Android应用程序,我发现我有三个不同的活动,比如活动A,活动B和活动C.我现在要做的是创建一个导航抽屉以在它们之间导航.我在Android开发者网站上阅读了该教程,但他们只关注片段.专业的Android应用程序如何仅使用一个Activity开发,所有其他屏幕都是使用Fragments开发的?如果没有记录,为什么没有记录如何使用Activities实现正确的导航抽屉呢?感谢您的帮助.

android android-fragments android-activity android-navigation

8
推荐指数
1
解决办法
8627
查看次数

Slider Menu片段中的可交换选项卡

我已经通过参考本教程实现了导航抽屉,现在我想要做的是在片段内显示滑动标签.即当我点击导航抽屉中的一个项目时,让我们说第一个项目,它应显示该项目的滑动标签.(我的列表视图中有6个片段.(同样: - A,B,C,D,E, F).我在A Fragment上需要3个可交换的标签.)

如果item1是Events,当我点击它时,它应该显示滑动标签.

但我面临以下问题: -

  1. 如何在片段内实现视图寻呼机?

  2. 片段可能扩展片段.

  3. 当一个片段正在扩展FragmentActivity时,那么在我的主要活动中,代码显示错误,说"类型不匹配",无法从A Fragment转换为片段.

这是我的Deep.java文件

    package com.amar.demo;

import info.androidhive.slidingmenu.adapter.NavDrawerListAdapter;
import info.androidhive.slidingmenu.model.NavDrawerItem;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class Deep extends Activity {
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    // nav drawer title
    private CharSequence mDrawerTitle;

    // used to store app title
    private …
Run Code Online (Sandbox Code Playgroud)

android android-sliding android-fragments android-tabs android-navigation

8
推荐指数
1
解决办法
9679
查看次数

工具栏setNavigationOnClickListener中断ActionbarDrawerToggle功能

我正在换掉工具栏的操作栏,我几乎已经完成了拼图的每一部分.我的问题是,如果我向上导航并恢复导航抽屉,抽屉切换按钮不再有效.我想出如果我将抽屉模式设置为解锁我有能力滑动打开抽屉,但无法点击打开抽屉.

所以我加载片段A,抽屉行为没问题,下到片段B并应用向上图标,点击返回A,抽屉不再打开,只需点击一下.

输入片段B:

Toolbar t = mHostingActivity.getToolbar();
        mHostingActivity.getDrawerToggle().setDrawerIndicatorEnabled(false);
        mHostingActivity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        t.setNavigationIcon(mHostingActivity.getV7DrawerToggleDelegate().getThemeUpIndicator());
        t.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popBackStackToTop(mHostingActivity);
            }
        });

/**
 * Pop the back stack and hide the Up caret all the way to the top level of the {@link com.loylap.activities.MainActivity}
 *
 * @param activity our hosting activity
 */
public static void popBackStackToTop(MainActivity activity) {
    if (activity != null) {
        FragmentManager fm = activity.getSupportFragmentManager();
        fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        activity.getDrawerLayout().setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
        activity.getDrawerToggle().setDrawerIndicatorEnabled(true);
    }
}
Run Code Online (Sandbox Code Playgroud)

导航抽屉的设置就像样品一样,也许设置选项的旧方法是问题?例如,我的活动中仍然有这个:

@Override
public boolean onOptionsItemSelected(MenuItem item) { …
Run Code Online (Sandbox Code Playgroud)

android android-navigation android-toolbar

8
推荐指数
3
解决办法
2万
查看次数

Android Studio 3.2 canary build不会渲染Navigation标签

Android studio 3.2 canary 14没有渲染导航标签.它只是显示等待构建完成.

navigation_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">
    <fragment
        android:id="@+id/fragment_nav_host"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/navigation_graph" />
</navigation>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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

8
推荐指数
1
解决办法
688
查看次数

使用导航组件,使用嵌套在其他 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
查看次数

是否可以更改导航组件中的向上按钮图标?

我想更改与导航组件一起使用的 ActionBar 中的向上按钮图标。我尝试了几种选择,例如:

supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_arrow_left_blue_24dp)
Run Code Online (Sandbox Code Playgroud)

MainAcitivty

app:navigationIcon="@drawable/ic_arrow_left_blue_24dp"
Run Code Online (Sandbox Code Playgroud)

Toolbar.xml 文件中,似乎没有任何效果。

我有一个非常标准的设置

setSupportActionBar(appToolbar.toolbar)
setupActionBarWithNavController(this, navController)
Run Code Online (Sandbox Code Playgroud)

MainActivity:onCreate方法中调用。

我希望

supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_arrow_left_blue_24dp)
Run Code Online (Sandbox Code Playgroud)

会起作用,因为例如ActionBar通过调用禁用标题:

supportActionBar?.setDisplayShowTitleEnabled(false)
Run Code Online (Sandbox Code Playgroud)

按预期工作,Fragment导航时标题未设置为名称。

更重要的是,我进行了一些调查,ActionBarOnDestinationChangedListener其中有一个调用将setNavigationIcon图标设置为DrawerArrowDrawable,这有点奇怪,因为我没有Drawer在我的设置中使用。

也改为:

setupWithNavController(toolbar, navController)
Run Code Online (Sandbox Code Playgroud)

不工作,因为ToolbarOnDestinationChangedListener也使用相同的DrawerArrowDrawable.

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

8
推荐指数
1
解决办法
2234
查看次数

如何使用导航组件找出前一个片段的内容

我试图找出前一个片段在 backstack 中的内容,如果它是我想要的片段,则返回到那个片段,否则转到另一个片段。我在查找之前片段的名称/ID 时遇到问题。我目前的设置如下:

Fragment 1 -> Fragment 2 -> Fragment 3 如果 Fragment 3 的前一个 id 是 >Fragment 2,则返回它并带参数。

但我也会遇到这种情况:

Fragment 4 -> Fragment 3 这里我还希望能够检查 Fragment 3 的前一个 id/name 是否等于 Fragment 4,以及它是否返回到带有参数的 Fragment。

基本上片段 3 会有不同的路径,我希望能够确定它接下来将转到哪个前一个片段。

我的问题是我无法访问以前 Fragments 的信息。检查是否有任何可能。我是使用 Android 导航组件的新手,因此不胜感激。

如果问题看起来有点令人困惑,请告诉我,以便我可以在需要时重写它。

谢谢!

android android-fragments kotlin android-navigation

8
推荐指数
1
解决办法
1585
查看次数

Android 导航组件片段过渡具有白色背景

我正在使用 Jetpack Navigation 库并且在片段转换方面遇到问题,无论是淡入淡出还是幻灯片动画,它始终具有白色背景而不是前一个屏幕内容。

片段 A -> 片段 B,当动画正在运行而不是将背景视为片段 A 时,它是白色背景。

导航

  <fragment
      android:id="@+id/nav_page"
      android:name="my.fragmenta"
      tools:layout="@layout/fragment_a">

    <action
        android:id="@+id/action_to_b"
        app:destination="@id/nav_b"
        app:enterAnim="@anim/slide_in_up"
        app:popExitAnim="@anim/slide_out_down"/>
  </fragment>
Run Code Online (Sandbox Code Playgroud)

活动方式

  <style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:statusBarColor">@color/statusBarColor</item>
  </style>
Run Code Online (Sandbox Code Playgroud)

动画文件

<translate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromYDelta="100%p"
    android:toYDelta="0"
    android:duration="@android:integer/config_longAnimTime" />
Run Code Online (Sandbox Code Playgroud)

<translate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromYDelta="0"
    android:toYDelta="100%p"
    android:duration="@android:integer/config_longAnimTime" />
Run Code Online (Sandbox Code Playgroud)

任何人都知道如何解决它?

android android-fragments android-navigation android-jetpack android-jetpack-navigation

8
推荐指数
1
解决办法
1373
查看次数

带有导航组件的滑动动画

我正在使用 JetPack 导航组件在片段之间导航。我在图表 xml 中的 2 个片段之间添加了幻灯片动画:

<action
    android:id="@+id/action_Fragment_to_DetailsFragment"
    app:enterAnim="@anim/slide_left"
    app:popEnterAnim="@anim/slide_right"
    app:destination="@id/DetailsFragment" />
Run Code Online (Sandbox Code Playgroud)

问题是 - b/c 我正在使用导航组件,它使用fragmentManager.replace(), 而不是fragmentManager.add(), 而不是平滑动画,我看到:

  1. 第一个片段消失了

  2. 正在触发第二个片段的动画并将新片段滑动到屏幕

  3. 第二个片段出现在屏幕上。

并且 b/c 我在两个屏幕上都有不同的内容,它看起来有问题。我想要实现的是“就像在 IOS 中一样”,用户看到 2 层屏幕,彼此滑动。有没有办法用不支持“ fragmentManager.add()”的导航组件来实现它?

我也试过

app:enterAnim="@anim/slide_in_right"
app:exitAnim="@anim/slide_out_left"
app:popEnterAnim="@anim/slide_in_left"
app:popExitAnim="@anim/slide_out_right"
Run Code Online (Sandbox Code Playgroud)

但情况也好不到哪里去。

android android-animation android-fragments android-navigation

8
推荐指数
2
解决办法
6344
查看次数

如何使用Android导航而不绑定到ViewModel(MVVM)中的UI?

我正在使用在Google I/O 2018上呈现的Android导航,似乎我可以通过绑定到某个视图或使用NavHost从Fragment获取它来使用它.但我需要的是根据几个条件从我的第一个片段导航到ViewModel中的另一个特定视图.因为ViewModel,我延伸AndroidViewModel,但我不明白下一步该怎么做.我无法转换getApplication为Fragment/Activity而我无法使用NavHostFragment.此外,我不能只导航到导航,onClickListener因为startFragment只包含一个导航ImageView.我该如何导航ViewModel

class CaptionViewModel(app: Application) : AndroidViewModel(app) {
private val dealerProfile = DealerProfile(getApplication())
val TAG = "REGDEB"


 fun start(){
    if(dealerProfile.getOperatorId().isEmpty()){
        if(dealerProfile.isFirstTimeLaunch()){
            Log.d(TAG, "First Time Launch")
            showTour()
        }else{
            showCodeFragment()
            Log.d(TAG, "Show Code Fragment")

        }
    }
}

private fun showCodeFragment(){
    //??
}

private fun showTour(){
    //??
}

}
Run Code Online (Sandbox Code Playgroud)

我的片段

class CaptionFragment : Fragment() {
private lateinit var viewModel: CaptionViewModel
private val navController by …
Run Code Online (Sandbox Code Playgroud)

android mvvm kotlin android-navigation android-jetpack

7
推荐指数
1
解决办法
2953
查看次数