2016-03-18 28 views
0

你好,我是新來的機器人。我想實現由一系列項目組成的導航抽屜,點擊後會打開一個新的活動。基本上是所有活動的導航抽屜。當我從導航抽屜中選擇一個項目時,特定活動將打開,但導航抽屜(漢堡包圖標)的圖標消失。我如何獲得?抽屜式導航欄的多項活動

NavBaseActivity.java

package app.pal.study.samplestudy; 

import android.content.Intent; 
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.Menu; 
import android.view.MenuItem; 
import android.widget.FrameLayout; 

public class NavBaseActivity extends AppCompatActivity 
    implements NavigationView.OnNavigationItemSelectedListener { 
private DrawerLayout drawer; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_nav_base); 

    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.setDrawerListener(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(); 
    } 
    // getActionBar().setDisplayHomeAsUpEnabled(true); 
    //getActionBar().setHomeButtonEnabled(true); 
} 

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

@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(); 


    //noinspection SimplifiableIfStatement 
    /* if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item);*/ 
    switch (item.getItemId()) { 
     case android.R.id.home: 
      drawer.openDrawer(GravityCompat.START); 
      return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

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

    if (id == R.id.item_home) { 
     // Handle the camera action 
     startActivity(new Intent(this, Home.class)); 

    } else if (id == R.id.item_syllabus) { 
     startActivity(new Intent(this, Syllabus.class)); 


    } else if (id == R.id.item_study_time) { 
     startActivity(new Intent(this, StudyTime.class)); 


    } else if (id == R.id.item_exam) { 
     startActivity(new Intent(this, Exam.class)); 


    } else if (id == R.id.item_calendar) { 
     startActivity(new Intent(this, Calendar.class)); 


    } else if (id == R.id.nav_scribble) { 
     startActivity(new Intent(this, Scribble.class)); 


    } 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    drawer.closeDrawer(GravityCompat.START); 
    return true; 
} 
// `onPostCreate` called when activity start-up is complete after `onStart()` 
// NOTE! Make sure to override the method with only a single `Bundle` argument 
@Override 
protected void onPostCreate(Bundle savedInstanceState) { 
    super.onPostCreate(savedInstanceState); 
} 

}

Syllabus.java

package app.pal.study.samplestudy; 

import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.NavigationView; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 

public class Syllabus extends NavBaseActivity implements   NavigationView.OnNavigationItemSelectedListener { 

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

    FloatingActionButton fab = (FloatingActionButton)   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(); 
     } 
    }); 
} 

}

activity_nav_base.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 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/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:openDrawer="start"> 

<include 
    layout="@layout/app_bar_nav_base" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

<android.support.design.widget.NavigationView 
    android:id="@+id/nav_view" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:fitsSystemWindows="true" 
    app:headerLayout="@layout/nav_header_nav_base" 
    app:menu="@menu/activity_nav_base_drawer" /> 

    </android.support.v4.widget.DrawerLayout> 

回答

1

它建議使用抽屜式導航與片段,因爲它會保持資產淨值抽屜。你必須將你的活動轉換成碎片。請檢查開發商鏈路http://developer.android.com/training/implementing-navigation/nav-drawer.html

+1

謝謝你。但是有沒有辦法可以修改我已經完成的這個程序。 – PersianBlue

+1

@Khadija - 當你發現錯誤時更正你的程序,而不是繼續以錯誤的方式執行,這也會導致你遇到其他問題。 – Passiondroid

+0

好的,我會用碎片。非常感謝你 – PersianBlue

0

有沒有人想出如何得到一個主要活動片段與推背按鈕來解決這個方法?

一切正常去「前進」,所有的片段被拉入的內容框架,但只要你打後退按鈕,該應用程序關閉...最有可能的,因爲它運行的唯一活動和後退按鈕關閉活動。

反正把代碼中得到片段經理回到過去的片段,而不是關閉後退按鈕的活動?

+0

'addToBackStack'將有所幫助 – Prabs