【01】NavHostFragment的使用

(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);
    }
});
FragmentContainerView 是一个用于承载 Fragment 的布局容器,它可以替代旧版的 FragmentActivity 和 FrameLayout 组合的方式。 不使用 NavHostFragment 的情况下,可以通过以下步骤来使用 FragmentContainerView。 1. 在布局文件中添加 FragmentContainerView,例如: ``` <androidx.fragment.app.FragmentContainerView android:id="@+id/fragment_container" android:name="com.example.MyFragment" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 其中,android:name 指定了要显示的 Fragment 类名。 2. 在 Activity 或 Fragment使用 FragmentManager 加载 Fragment,例如: ``` val fragmentManager = supportFragmentManager val fragmentTransaction = fragmentManager.beginTransaction() val myFragment = MyFragment() fragmentTransaction.replace(R.id.fragment_container, myFragment) fragmentTransaction.commit() ``` 其中,R.id.fragment_container 是 FragmentContainerView 的 ID。 3. 如果需要在 Fragment使用 ViewModel,可以使用 ViewModelProvider 来创建和获取 ViewModel,例如: ``` val viewModel = ViewModelProvider(this).get(MyViewModel::class.java) ``` 其中,this 是 Fragment,如果是 Activity 则需要传入 this@ActivityName。 4. 如果需要向 Fragment 传递参数,可以使用 Bundle,例如: ``` val args = Bundle() args.putString("key", "value") myFragment.arguments = args ``` 在 Fragment 中获取参数: ``` val args = arguments val value = args?.getString("key") ``` 以上就是使用 FragmentContainerView 的基本流程,希望对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值