(1)一个人只要自己不放弃自己,整个世界也不会放弃你.
(2)天生我才必有大用
(3)不能忍受学习之苦就一定要忍受生活之苦,这是多么痛苦而深刻的领悟.
(4)做难事必有所得
(5)精神乃真正的刀锋
(6)战胜对手有两次,第一次在内心中.
(7)好好活就是做有意义的事情.
(8)亡羊补牢,为时未晚
(9)科技领域,没有捷径与投机取巧。
(10)有实力,一年365天都是应聘的旺季,没实力,天天都是应聘的淡季。
(11)基础不牢,地动天摇
(12)写博客初心:成长自己,辅助他人。当某一天离开人世,希望博客中的思想还能帮人指引方向.
(13)编写实属不易,若喜欢或者对你有帮助记得点赞+关注或者收藏哦~
NavHostFragment的使用
文章目录
1.引入依赖库
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
def nav_version = '2.1.0';
implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"
}
2.创建多个Fragment与布局
(1)Fragment1、Fragment2、Fragment3…
(2)layout_fragment1.xml、layout_fragment2.xml、layout_fragment3.xml…
3.activity中的NavHostFragment容器
<?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">
<!--
1.app:defaultNavHost="true"
1.1为true的时候,拦截系统back键,在fragment中可以用back回退到上一个fragment
1.2为false的时候,不拦截系统的back键,按back键直接退出Activity
2.app:navGraph="@navigation/nav_graph_main"
2.1连接使用nav_graph_main.xml,获取fragment之间的导航结构
-->
<fragment
android:id="@+id/fragment_nav_host"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="androidx.navigation.fragment.NavHostFragment"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/nav_graph_main"
app:defaultNavHost="true"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
3.配置各个fragment之间的导航结构
(1)在res下创建navigation文件夹
(2)创建导航结构文件,配置好fragment之间的跳转
<?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"
app:startDestination="@id/fragment1"
android:id="@+id/nav_graph_main">
<fragment
android:id="@+id/fragment1"
android:name="com.gdc.livedatabus.fragments.MainFragment1"
android:label="fragment1"
tools:layout="@layout/fragment_main_fragment1">
<!--
action:表示程序中使用id跳到destination对应的类
-->
<action
android:id="@+id/action_page2"
app:destination="@+id/fragment2"/>
</fragment>
<fragment
android:id="@+id/fragment2"
android:name="com.gdc.livedatabus.fragments.MainFragment2"
android:label="fragment2"
tools:layout="@layout/fragment_main2">
<action
android:id="@+id/action_page1"
app:destination="@id/fragment1"/>
<action
android:id="@+id/action_page3"
app:destination="@id/fragment3"/>
</fragment>
<fragment
android:id="@+id/fragment3"
android:name="com.gdc.livedatabus.fragments.MainFragment3"
android:label="fragment3"
tools:layout="@layout/fragment_main3">
<action android:id="@+id/action_page2"
app:destination="@id/fragment2"/>
</fragment>
</navigation>
4.Activity中加载
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onSupportNavigateUp() {
Fragment fragment =
getSupportFragmentManager().findFragmentById(R.id.fragment_nav_host);
return NavHostFragment.findNavController(fragment).navigateUp();
}
}
5.Fragment导航跳转
Button button = view.findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Navigation.findNavController(view).navigate(R.id.action_page2);
Person p = new Person("张三丰","男",22);
LiveDataBus.getInstance().with("person",Person.class).postValue(p);
}
});
.action_page2);
Person p = new Person("张三丰","男",22);
LiveDataBus.getInstance().with("person",Person.class).postValue(p);
}
});