gpt4 book ai didi

java - 如何使用Android抽屉导航切换回Main Activity

转载 作者:太空宇宙 更新时间:2023-11-04 10:59:14 26 4
gpt4 key购买 nike

您好,我正在开发一个程序,并使用 Android Studio 为您提供的抽屉导航 Activity 模板制作了一个抽屉导航,这是我的主要 Activity 。我一直在使用 fragment 在应用程序中的页面之间切换,但我认为主 Activity 是抽屉中的选项之一,但我似乎不知道如何使其返回主 Activity。

这是主要的 Activity.java 文件(为了组织而重命名)

package com.eliteapp.eliteapp_indev;

import android.app.FragmentManager;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

public class your_cmdrs extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_cmdrs);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}

@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();


return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
switch (id){


}

if (id == R.id.GalNet) {
setTitle("GalNet");
galnet GalNat = new galnet();
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction(). replace(R.id.Your_CMDRS, GalNat).commit();
} else if (id == R.id.Market) {
setTitle("Market");
market Market = new market();
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction(). replace(R.id.Your_CMDRS, Market).commit();
} else if (id == R.id.PowerPlay) {
setTitle("PowerPlay");
powrplay PowrPlay = new powrplay();
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction(). replace(R.id.Your_CMDRS, PowrPlay).commit();


} else if (id == R.id.ship_fitting) {
setTitle("Ship Fitting's");
ship_fitting Ship_Fitting = new ship_fitting();
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction(). replace(R.id.Your_CMDRS, Ship_Fitting).commit();
} else if (id == R.id.About) {
setTitle("About");
about About = new about();
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction(). replace(R.id.Your_CMDRS, About).commit();
} else if (id == R.id.Settings) {
setTitle("Settings");
settings Settings = new settings();
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction(). replace(R.id.Your_CMDRS, Settings).commit();
} else if (id == R.id.Change_Log) {
setTitle("Change Log");
change_log Chagne_Log = new change_log();
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction(). replace(R.id.Your_CMDRS, Chagne_Log).commit();
}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}

这是 content_Main.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:id="@+id/Your_CMDRS"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.eliteapp.eliteapp_indev.your_cmdrs"
tools:showIn="@layout/app_bar_your_cmdrs">

</android.support.constraint.ConstraintLayout>

如果有人能帮助我解决这个问题,那就太好了。

最佳答案

@AJok1776我面临着同样的问题。

解决方案是您创建一个 MainFragment.java 与您的 MainActivity 相同的内容(不包括抽屉导航)默认加载 MainFragment 页面。

MainActivity 内容是唯一的抽屉导航代码。

然后在用户单击主页时在抽屉中添加主页按钮(或其他),您将重定向 MainFragment 页面。

希望这个解释对你有帮助:)

关于java - 如何使用Android抽屉导航切换回Main Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47088414/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com