导航抽屉活动的界面跳转

navigation drawer activity's interface jumps

提问人:liuhua 提问时间:10/30/2023 更新时间:10/30/2023 访问量:10

问:

既有界面又有导航栏,实现了导航栏与界面之间的链接,点击导航栏即可跳转到界面。

mobile_navigation.xml

<?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/mobile_navigation"
    app:startDestination="@+id/nav_home">

    <fragment
        android:id="@+id/nav_home"
        android:name="android.example.setthestorm.ui.home.HomeFragment"
        android:label="@string/dengluzhuce"
        tools:layout="@layout/fragment_home">
        <action
            android:id="@+id/action_nav_home_to_login_registration"
            app:destination="@id/login_registration" />
        <action
            android:id="@+id/action_nav_home_to_appreciation_of_poetry2"
            app:destination="@id/appreciation_of_poetry" />
        <action
            android:id="@+id/action_nav_home_to_poetry_details2"
            app:destination="@id/poetry_details" />
        <action
            android:id="@+id/action_nav_home_to_poetry_search"
            app:destination="@id/poetry_search" />
        <action
            android:id="@+id/action_nav_home_to_category_browsing"
            app:destination="@id/category_browsing" />
        <action
            android:id="@+id/action_nav_home_to_one_sentence_a_day"
            app:destination="@id/one_sentence_a_day" />
        <action
            android:id="@+id/action_nav_home_to_learning_record"
            app:destination="@id/learning_record" />
<!--        <action-->
<!--            android:id="@+id/action_nav_home_to_personal_center"-->
<!--            app:destination="@id/personal_center" />-->

    </fragment>
    <fragment
        android:id="@+id/login_registration"
        android:name="android.example.setthestorm.login_registration"
        android:label="fragment_login_registration"
        tools:layout="@layout/fragment_login_registration" />
    <fragment
        android:id="@+id/appreciation_of_poetry"
        android:name="android.example.setthestorm.appreciation_of_poetry"
        android:label="fragment_appreciation_of_poetry"
        tools:layout="@layout/fragment_appreciation_of_poetry" />
    <fragment
        android:id="@+id/poetry_details"
        android:name="android.example.setthestorm.poetry_details"
        android:label="fragment_poetry_details"
        tools:layout="@layout/fragment_poetry_details" />
    <fragment
        android:id="@+id/poetry_search"
        android:name="android.example.setthestorm.poetry_search"
        android:label="fragment_poetry_search"
        tools:layout="@layout/fragment_poetry_search" />
    <fragment
        android:id="@+id/category_browsing"
        android:name="android.example.setthestorm.category_browsing"
        android:label="fragment_category_browsing"
        tools:layout="@layout/fragment_category_browsing" />
    <fragment
        android:id="@+id/one_sentence_a_day"
        android:name="android.example.setthestorm.one_sentence_a_day"
        android:label="fragment_one_sentence_a_day"
        tools:layout="@layout/fragment_one_sentence_a_day" />
    <fragment
        android:id="@+id/learning_record"
        android:name="android.example.setthestorm.learning_record"
        android:label="fragment_learning_record"
        tools:layout="@layout/fragment_learning_record" />

<!--    <fragment-->
<!--        android:id="@+id/personal_center"-->
<!--        android:name="android.example.setthestorm.personal_center"-->
<!--        android:label="fragment_personal_center"-->
<!--        tools:layout="@layout/fragment_personal_center" />-->


</navigation>

既有界面又有导航栏,实现了导航栏与界面之间的链接,点击导航栏即可跳转到界面。

主要活动

package android.example.setthestorm;

import android.os.Bundle;
import android.view.View;
import android.view.Menu;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

public class MainActivity extends AppCompatActivity {

    private AppBarConfiguration mAppBarConfiguration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.login_registration,R.id.appreciation_of_poetry,R.id.poetry_details,R.id.poetry_search,R.id.category_browsing,R.id.one_sentence_a_day,R.id.learning_record)  //,R.id.personal_center
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }


}

xml android-studio

评论


答: 暂无答案