Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Navigation in Android (一) #114

Open
soapgu opened this issue Feb 28, 2022 · 0 comments
Open

Navigation in Android (一) #114

soapgu opened this issue Feb 28, 2022 · 0 comments
Labels
Demo Demo 安卓 安卓

Comments

@soapgu
Copy link
Owner

soapgu commented Feb 28, 2022

  • 引子

其实一开始学习的安卓的时候就接触到Navigation了,当初体验并不佳,再说对安卓也不是太熟悉。所以就用其他方案代替了。这次正好有用到导航控件,无奈与导航是“捆绑销售”的。强行拆开就失去了设计者的本身意图了,所以还要重新拾起来了

  • Quick Start

1.首先需要添加依赖

def nav_version = "2.4.1"

  // Java language implementation
  implementation "androidx.navigation:navigation-fragment:$nav_version"
  implementation "androidx.navigation:navigation-ui:$nav_version"

在build.gradle里面增加,目前稳定版是2.4.1

  1. 创建导航图

    在“Project”窗口中,右键点击 res 目录,然后依次选择 New > Android Resource File。此时系统会显示 New Resource File 对话框。
    在 File name 字段中输入名称,例如“nav_graph”。
    从 Resource type 下拉列表中选择 Navigation,然后点击 OK。

  2. 创建目的地
    可以通过现有的Fragment,DialogFragment,activity创建目的地
    可以通过ide添加目的地,也可以通过房子的图标设置目的的“出发点”

  3. 创建NavHostFragment

<androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragmentContainerView"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_graph" />

创建一个FragmentContainerView,把name设为"androidx.navigation.fragment.NavHostFragment",同时把app:navGraph关联到我们前面用的导航图

好了,运行一下最简版的导览已经有了,当然只能显示首页

参考

导航关键是要能“导”

  1. 我们要找到导航的“引擎”——NavController

我们可以通过以下api去找到他

        binding.btnSpace.setOnClickListener(v -> Navigation.findNavController(v).navigate(R.id.spaceFragment));

如下代码实现了导航到spaceFragment的功能,当然这是最简单的方式

  • 创建底部导航导航栏

  1. 在界面上增加BottomNavigationView
<com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/menu_bottom_nav" />
  1. 新增menu resource
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/homeFragment"
        android:icon="@android:drawable/ic_menu_myplaces"
        android:title="Home" />
    <item
        android:id="@+id/spaceFragment"
        android:icon="?attr/actionModeCopyDrawable"
        android:title="Space" />

    <item
        android:id="@+id/myFragment"
        android:icon="@android:drawable/btn_star_big_on"
        android:title="我的" />
</menu>

注意:这里的id需要和nav_graph.xml做好“梦幻联动”,id必须一致

  1. 代码部分,设置BottomNavigationView和NavController的关联
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        NavHostFragment navHostFragment = (NavHostFragment)getSupportFragmentManager().findFragmentById(R.id.fragmentContainerView);
        NavController navController = navHostFragment.getNavController();
        BottomNavigationView bottomNav = findViewById(R.id.bottom_nav);
        NavigationUI.setupWithNavController(bottomNav, navController);
    }
  1. Add Listerner
navController.addOnDestinationChangedListener( (controller,destination,arguments) ->{
            Logger.i( "DestinationChanged:%s",destination.getDisplayName());
            switch (destination.getId() ) {
                case R.id.homeFragment:
                    Logger.i("go to home");
                    break;
                case R.id.spaceFragment:
                    Logger.i("go to space");
                    break;
                case R.id.myFragment:
                    Logger.i("go to my");
                    break;
                default:
                    Logger.i("other...");
            }
        });

addOnDestinationChangedListener可以提供对导航切换的控制操作

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Demo Demo 安卓 安卓
Projects
None yet
Development

No branches or pull requests

1 participant