2015-09-21 42 views
1

我使用材質設計導航視圖..我創建菜單項和鏈接的活動(Activity2.java)與項目(加星標)...並在該活動中我擴展我的主要活動,其中有導航視圖,以便我也可以從該活動中滑動導航視圖..但是當我從第二個活動中滑動導航視圖時,該項目未被選中,如果我按下後退按鈕並轉到Main活動,則顯示以前檢查過的菜單項...如何在其他activity.please幫助更新的檢查項目導航查看選定菜單項檢入其他活動?

MainActivity.java

public class MainActivity extends AppCompatActivity { 

    //Defining Variables 
    private Toolbar toolbar; 
    private NavigationView navigationView; 
    private DrawerLayout drawerLayout; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // Initializing Toolbar and setting it as the actionbar 
     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     //Initializing NavigationView 
     navigationView = (NavigationView) findViewById(R.id.navigation_view); 

     //Setting Navigation View Item Selected Listener to handle the item click of the navigation menu 
     navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 

      // This method will trigger on item Click of navigation menu 
      @Override 
      public boolean onNavigationItemSelected(MenuItem menuItem) { 


       //Checking if the item is in checked state or not, if not make it in checked state 
       if(menuItem.isChecked()) menuItem.setChecked(false); 
       else menuItem.setChecked(true); 

       //Closing drawer on item click 
       drawerLayout.closeDrawers(); 

       //Check to see which item was being clicked and perform appropriate action 
       switch (menuItem.getItemId()){ 


        //Replacing the main content with ContentFragment Which is our Inbox View; 
        case R.id.inbox: 
         Toast.makeText(getApplicationContext(),"Inbox Selected",Toast.LENGTH_SHORT).show(); 
         ContentFragment fragment = new ContentFragment(); 
         android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
         fragmentTransaction.replace(R.id.frame,fragment); 
         fragmentTransaction.commit(); 
         return true; 

        // For rest of the options we just show a toast on click 

        case R.id.starred: 

         startActivity(new Intent(MainActivity.this,Activity2.class)); 
         drawerLayout.closeDrawers(); 
         Toast.makeText(getApplicationContext(),"Stared Selected",Toast.LENGTH_SHORT).show(); 
         return true; 
        case R.id.sent_mail: 
         Toast.makeText(getApplicationContext(),"Send Selected",Toast.LENGTH_SHORT).show(); 
         return true; 
        case R.id.drafts: 
         Toast.makeText(getApplicationContext(),"Drafts Selected",Toast.LENGTH_SHORT).show(); 
         return true; 
        case R.id.allmail: 
         Toast.makeText(getApplicationContext(),"All Mail Selected",Toast.LENGTH_SHORT).show(); 
         return true; 
        case R.id.trash: 
         Toast.makeText(getApplicationContext(),"Trash Selected",Toast.LENGTH_SHORT).show(); 
         return true; 
        case R.id.spam: 
         Toast.makeText(getApplicationContext(),"Spam Selected",Toast.LENGTH_SHORT).show(); 
         return true; 
        default: 
         Toast.makeText(getApplicationContext(),"Somethings Wrong",Toast.LENGTH_SHORT).show(); 
         return true; 

       } 
      } 
     }); 

     // Initializing Drawer Layout and ActionBarToggle 
     drawerLayout = (DrawerLayout) findViewById(R.id.drawer); 
     ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.openDrawer, R.string.closeDrawer){ 

      @Override 
      public void onDrawerClosed(View drawerView) { 
       // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank 
       super.onDrawerClosed(drawerView); 
      } 

      @Override 
      public void onDrawerOpened(View drawerView) { 
       // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank 

       super.onDrawerOpened(drawerView); 
      } 
     }; 

     //Setting the actionbarToggle to drawer layout 
     drawerLayout.setDrawerListener(actionBarDrawerToggle); 

     //calling sync state is necessay or else your hamburger icon wont show up 
     actionBarDrawerToggle.syncState(); 






    } 
} 

Activity2.java

public class Activity2 extends MainActivity{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     FrameLayout frameLayout = (FrameLayout)findViewById(R.id.frame); 
     // inflate the custom activity layout 
     LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View activityView = layoutInflater.inflate(R.layout.activity_2, null,false); 
     // add the custom layout of this activity to frame layout. 
     frameLayout.addView(activityView); 
    } 
} 

activity_main.xml中

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/drawer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context=".MainActivity"> 

    <LinearLayout 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:orientation="vertical" 
     > 
     <include 
      android:id="@+id/toolbar" 
      layout="@layout/tool_bar" 
     /> 
     <FrameLayout 
      android:id="@+id/frame" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

     </FrameLayout> 

    </LinearLayout> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/navigation_view" 
     android:layout_height="match_parent" 
     android:layout_width="wrap_content" 
     android:layout_gravity="start" 
     app:headerLayout="@layout/header" 
     app:menu="@menu/drawer" 
     /> 
</android.support.v4.widget.DrawerLayout> 

drawer.xml(menu_drawer)

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 

    <group android:checkableBehavior="single"> 

     <item 
      android:id="@+id/inbox" 
      android:checked="false" 
      android:icon="@drawable/ic_inbox_black" 
      android:title="@string/inbox_string" /> 

     <item 
      android:id="@+id/starred" 
      android:checked="false" 
      android:icon="@drawable/ic_star_black" 
      android:title="@string/starred_string" /> 

     <item 
      android:id="@+id/sent_mail" 
      android:checked="false" 
      android:icon="@drawable/ic_send_black" 
      android:title="@string/sent_mail_string" /> 

     <item 
      android:id="@+id/drafts" 
      android:checked="false" 
      android:icon="@drawable/ic_drafts_black" 
      android:title="@string/draft_string" /> 


     <item 
      android:id="@+id/allmail" 
      android:checked="false" 
      android:icon="@drawable/ic_email_black" 
      android:title="@string/all_mail_string" /> 
     <item 
      android:id="@+id/trash" 
      android:checked="false" 
      android:icon="@drawable/ic_delete_black" 
      android:title="@string/trash_string" /> 
     <item 
      android:id="@+id/spam" 
      android:checked="false" 
      android:icon="@drawable/ic_error_black" 
      android:title="@string/spam_string" /> 

    </group> 
</menu> 
+0

你能否提供導航抽屜的xml代碼? – Saibbyweb

+0

完成!你知道如何去做 –

回答

0

試試這個在您的活動2:

@Override 
    public boolean onPrepareOptionsMenu(Menu menu) { 
     super.onPrepareOptionsMenu(menu); 
     menu.findItem(R.id.starred).setChecked(true); 
     return true; 
    } 
+0

*錯誤*嘗試調用null對象引用的接口方法'android.view.MenuItem android.view.MenuItem.setChecked(boolean)' –

+0

您必須在navigationView而不是菜單中搜索menuItems。看到我的答案。 – mohax

4
  1. 添加protected int變量存儲選中的菜單項的ID
  2. 從NavigationView的clickListener將選中的菜單項ID添加到intent的extras;開始你的第二個活動與此意圖
  3. 在第二活動得到額外的菜單項ID,並將其設置爲類變量從(參見圖1)
  4. 在你添加onPrepareOptionsMenu在你的超級活動

    @Override 
    public boolean onPrepareOptionsMenu(Menu menu) { 
        super.onPrepareOptionsMenu(menu); 
        //recreate navigationView's menu, uncheck all items and set new checked item 
        navigationView.getMenu().clear(); 
        navigationView.inflateMenu(R.menu.drawer); 
        //setChecked(false) to all yours menu items in NavigationView 
        navigationView.getMenu().findItem(R.id.SOME_ID_0).setChecked(false); 
        navigationView.getMenu().findItem(R.id.SOME_ID_1).setChecked(false); 
        navigationView.getMenu().findItem(R.id.SOME_ID_2).setChecked(false); 
        ...etc 
        navigationView.setCheckedItem(checkedDrawerItemId); 
    } 
    
  5. NavigationView的clickListener不要忘記將點擊項目的ID設置爲Activities類變量並調用supportInvalidateOptionsMenu();
+0

其實mohax我是新來的機器人,我不熟悉上面的東西..可能請詳細說明這些額外和所有 –