Android Jetpack基础——Navigation
程序员文章站
2022-03-10 22:08:14
...
实现效果如图
Navigation:有三个主要的部分:
Navigation graph:也就是Navigatation的xml文件,包含所有的需要跳转的目标
NavHost:一个容器,用于显示
NavController:控制跳转流程
具体实现
新建两个Fragment,HomeFragment和DetailFragment。
在Activity xml中为Fragment添加NavHost容器。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="@+id/fragment"
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/my_nav" />
</androidx.constraintlayout.widget.ConstraintLayout>
再新建navigation文件夹新建Navigation graph。添加Fragment到其中
现在便可以来具体实现跳转了。
package com.ywz.navigationdemo;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class HomeFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Button btn;
btn = getView().findViewById(R.id.home_btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
NavController controller = Navigation.findNavController(view);
controller.navigate(R.id.action_homeFragment_to_detailFragment);
}
});
}
}
package com.ywz.navigationdemo;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class DetailFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_detail, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Button btn;
btn = getView().findViewById(R.id.detail_btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
NavController controller = Navigation.findNavController(view);
controller.navigate(R.id.action_detailFragment_to_homeFragment);
}
});
}
}
MainActivity
package com.ywz.navigationdemo;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NavController controller = Navigation.findNavController(this, R.id.fragment);
NavigationUI.setupActionBarWithNavController(this, controller);
}
@Override
public boolean onSupportNavigateUp() {
NavController controller = Navigation.findNavController(this, R.id.fragment);
return controller.navigateUp();
}
}
推荐阅读
-
android开发基础知识点(有关android入门知识解析)
-
Android开发基础之创建启动界面Splash Screen的方法
-
Android开发之React Navigation 导航栏样式调整+底部角标消息提示
-
android基础教程之夜间模式实现示例
-
android基础教程之开机启动示例
-
android基础教程之android的listview与edittext冲突解决方法
-
关于Android开发中需要掌握的基础知识点讲述
-
Android基础控件RadioGroup使用方法详解
-
android基础教程之context使用详解
-
Android网络框架的优缺点、图片加载框架等基础知识讲解