当前位置:网站首页>Jetpack -- navigation realizes page Jump
Jetpack -- navigation realizes page Jump
2022-07-29 02:13:00 【Y.IU.】
Navigation Components can make switching between pages easier .
Navigation It roughly includes four parts :
NavHost
NavHost Equivalent to a container , Used to store which pages can be accessed , Which pages can go out .
Fragment
Fragment The original purpose of the launch is to adapt to the large screen , Divide the large screen into small parts , Each small part is a Fragment.
NavController
NavController Logic used to control navigation , Press the key to switch to which page , The specific page to switch to depends on the navigation route .
NavGraph
NavGraph Shows the logical relationship between pages , That is, the switching relationship between pages , There are arrows between pages , Each arrow is a Action, from NavController To drive these Action, Then realize the switching of pages .
Code practice
First step : First create two Fragment.
fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".fragment.HomeFragment" android:orientation="vertical" android:gravity="center" >
<TextView android:layout_width="100dp" android:layout_height="50dp" android:text=" Home page " android:gravity="center" />
<Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" Jump " />
</LinearLayout>
fragment_detail.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".fragment.HomeFragment" android:orientation="vertical" android:gravity="center" >
<TextView android:layout_width="100dp" android:layout_height="50dp" android:text=" Details page " android:gravity="center" />
<Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" Jump " />
</LinearLayout>
The second step : stay res Create under file Navigation Resource file , Used to design the roadmap (NavGraph).
have access to AS Provide visualization for design :
Click the green plus sign to add the page to be switched .
Each page is connected by arrows , Indicates that you can jump from a page to the page pointed by the arrow , Each arrow is a Action, Every Action There's a unique one id.
After that , We will find that Hosts The box will have the following prompt , Chinese meaning :“ The navigation map must start from NavHostFragment To access ”, This NavHostFragment Need to be in Activity Of xml Create in file , See Step 3 for details .
This is the code part
<?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/homeFragment">
<fragment android:id="@+id/homeFragment" android:name="com.example.navigationtest.fragment.HomeFragment" android:label="fragment_home" tools:layout="@layout/fragment_home" >
<action android:id="@+id/action_homeFragment_to_detailFragment" app:destination="@id/detailFragment" />
</fragment>
<fragment android:id="@+id/detailFragment" android:name="com.example.navigationtest.fragment.DetailFragment" android:label="DetailFragment" >
<action android:id="@+id/action_detailFragment_to_homeFragment2" app:destination="@id/homeFragment" />
</fragment>
</navigation>
The third step : establish NavHostFragment
stay activity_main.xml Choose from NavHostFragment Drag to the layout on the right .

Then there will be the following prompt , Select the navigation map you want to access , Click on “OK” that will do .

Returning to the navigation map file, we can see NavHostFragment It's connected .
The code is as follows :
<?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">
<androidx.fragment.app.FragmentContainerView android:id="@+id/fragmentContainerView2" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="409dp" android:layout_height="729dp" app:defaultNavHost="true" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:navGraph="@navigation/nav_graph" tools:layout_editor_absoluteY="1dp" tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step four : Use NavControler To control the logic of navigation .
First , The jump of the page must be triggered by the user clicking on a control or key , So when dealing with click events, realize page Jump . The core code is as follows :
NavController controller = Navigation.findNavController(v); // Get controller
controller.navigate(R.id.action_homeFragment_to_detailFragment); // call navigation Method to realize page Jump , This method needs to pass a Action Of id, Indicates that you want to execute this jump behavior .
HomeFragment
public class HomeFragment extends Fragment {
public HomeFragment() {
// Required empty public constructor
}
@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 onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
Button button = (Button) view.findViewById(R.id.btn1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NavController controller = Navigation.findNavController(v);
controller.navigate(R.id.action_homeFragment_to_detailFragment);
}
});
}
}
Use NavController The second way to write control navigation logic , This kind of writing is more concise .
button.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_detailFragment_to_homeFragment2));
DetailFragment
public class DetailFragment extends Fragment {
public DetailFragment() {
// Required empty public constructor
}
@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 onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
Button button = (Button) view.findViewById(R.id.btn1);
button.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_detailFragment_to_homeFragment2));
}
}
thus , The basic jump function has been realized , But if you want to click the fallback button in the upper left corner to realize the fallback function, how to realize it ? Of course ,Navigation It also provides us with this function , As follows :
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NavHostFragment fragment = (NavHostFragment)getSupportFragmentManager().findFragmentById(R.id.fragmentContainerView);
NavController controller = fragment.getNavController();
NavigationUI.setupActionBarWithNavController(this,controller);
}
@Override
public boolean onSupportNavigateUp() {
NavController controller = Navigation.findNavController(this,R.id.fragmentContainerView);
return controller.navigateUp();
}
}
边栏推荐
- 指针——黄金阶段
- (arxiv-2018) reexamine the time modeling of person Reid based on video
- 第十四天:续第十三天标签相关知识
- Leetcode exercise - Sword finger offer 45. arrange the array into the smallest number
- Click back to the top JS
- Comprehensive use method of C treeview control
- 费曼学习法(符号表)
- 【ONE·Data || 链式二叉树】
- 向量相似度评估方法
- Know that Chuangyu is listed in many fields of ccsip 2022 panorama
猜你喜欢

Establish an engineering template based on STM32 in keil -- detailed steps

How to crawl web pages with playwright?

TI C6000 TMS320C6678 DSP+ Zynq-7045的PS + PL异构多核案例开发手册(2)

Type analysis of demultiplexer (demultiplexer)

Navigation--实现Fragment之间数据传递和数据共享

为什么 BI 软件都搞不定关联分析

FPGA实现10M多功能信号发生器
![[UE4] replay game playback for ue4.26](/img/c3/1c7b30797f46dbd323cac4d158600f.png)
[UE4] replay game playback for ue4.26

指针——黄金阶段

In 2022, the official data of programming language ranking came, which was an eye opener
随机推荐
leetcode 242. Valid Anagram(有效的字母异位词)
[UE4] replay game playback for ue4.26
Mathematical modeling - location of police stations
2022.7.27-----leetcode.592
点击按钮,下滑到指定的位置
数学建模——红酒品质分类
The solution of reducing the sharpness of pictures after inserting into word documents
Probability Density Reweight
Know that Chuangyu is listed in many fields of ccsip 2022 panorama
Complete collection of common error handling in MySQL installation
Dynamic memory and smart pointer
数学建模——仓内拣货优化问题
JetPack--Navigation实现页面跳转
向量相似度评估方法
Wonderful use of data analysis
【云原生与5G】微服务加持5G核心网
Click back to the top JS
ciscn 2022 华中赛区 misc
(CVPR-2019)选择性的内核网络
Add graceful annotations to latex formula; "Data science" interview questions collection of RI Gai; College Students' computer self-study guide; Personal firewall; Cutting edge materials / papers | sh