您好我正在开发一个程序并使用Android Studio为您提供的导航抽屉活动模板创建了一个导航抽屉,这是我的主要活动。我一直在使用片段在我的应用程序中的页面之间切换,但我的主要活动是抽屉中的选项之一,但我似乎无法弄清楚如何让它返回主活动。
这是主要的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>
如果有人能帮助我,那就太好了。
答案 0 :(得分:0)
@ AJok1776我面临同样的问题。
解决方案是您创建一个与您的MainActivity相同的MainFragment.java
内容(不包括导航抽屉)默认情况下,加载MainFragment页面。
MainActivity内容是唯一的导航抽屉代码。
然后在用户点击主页时,在主页上添加主页按钮(或其他),重定向MainFragment页面。
希望这个解释可以帮助你:)
答案 1 :(得分:0)
修改强>
我设法通过将此代码添加到main.java文件中的if语句来使其工作。
} else if (id == R.id.Your_CMDRS) {
Intent your_cmdrs = new Intent(this, your_cmdrs.class);
startActivity(your_cmdrs);
现在它可以工作,但是当我切换回活动时会有延迟,即使我在主要活动中也可以切换到它
以下是该视频: https://youtu.be/lQ93OeZDxIY
我认为这是一个事实,它是一个活动,而不是片段,但我不确定。