2014-11-22 112 views
1

所以現在我正在開發一個Android應用程序,我試圖從NavigationDrawer切換到不同的活動。在我的MainActivity中,我設置了導航器抽屜,並在應用程序第一次打開時作爲主視圖/活動的StartingFragment。使用NavigationDrawer在活動之間導航?

但是,我無法做到這一點。每當我點擊導航抽屜中的任何列表項時,都不會切換任何活動。我查看了StackOverflow和其他網站的解決方案,但我似乎無法弄清楚這個問題。

public class MainActivity extends FragmentActivity { 

Button tea_type; 
private String[] navDrawerTitles; 
private DrawerLayout navDrawerLayout; 
private ListView mDrawerList; 
private ActionBarDrawerToggle mDrawerToggle; 
private CharSequence mTitle; 

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

    navDrawerTitles = getResources().getStringArray(R.array.nav_array); 
    navDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
    mDrawerList = (ListView) findViewById(R.id.left_drawer); 

    // Set the adapter for the list view 
    mDrawerList.setAdapter(new ArrayAdapter<String>(this, 
      R.layout.drawer_list_item, navDrawerTitles)); 

     // When a menu item is clicked 
    mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int position, long stuff) { 
      final FragmentManager manager = getSupportFragmentManager(); 
      final FragmentTransaction fragmentTransaction = manager.beginTransaction(); 
      Fragment frag; 
      // Depending on position, set your fragment 
      if (position == 0) { 
       frag = new StartingFragment(); 
       fragmentTransaction.add(R.id.starting_fragment, frag); 
       fragmentTransaction.addToBackStack(null); 

       // "Commits" the fragment to the fragment view in the layout 
       fragmentTransaction.commit(); 
      } 
     } 
    }); 

    navDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
    mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */ 
    navDrawerLayout, /* DrawerLayout object */ 
    R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */ 
    R.string.drawer_open, /* "open drawer" description */ 
    R.string.drawer_close /* "close drawer" description */ 
    ) { 

     /** Called when a drawer has settled in a completely closed state. */ 
     public void onDrawerClosed(View view) { 
      super.onDrawerClosed(view); 
      getActionBar().setTitle(R.string.app_name); 
     } 

     /** Called when a drawer has settled in a completely open state. */ 
     public void onDrawerOpened(View drawerView) { 
      super.onDrawerOpened(drawerView); 
      getActionBar().setTitle(R.string.app_name); 
     } 
    }; 

    // Set the drawer toggle as the DrawerListener 
    navDrawerLayout.setDrawerListener(mDrawerToggle); 

    getActionBar().setDisplayHomeAsUpEnabled(true); 
    getActionBar().setHomeButtonEnabled(true); 
} 

/* Called whenever we call invalidateOptionsMenu() */ 
@Override 
public boolean onPrepareOptionsMenu(Menu menu) { 
    // If the nav drawer is open, hide action items related to the content 
    // view 
    boolean drawerOpen = navDrawerLayout.isDrawerOpen(mDrawerList); 
    menu.findItem(R.id.action_settings).setVisible(!drawerOpen); 
    return super.onPrepareOptionsMenu(menu); 
} 

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; 
} 


public class DrawerItemClickListener implements 
     ListView.OnItemClickListener { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, 
      long id) { 
     selectItem(position); 

     } 
    } 

/** Swaps fragments in the main content view */ 
/** Starts an Activity when item is clicked*/ 
private void selectItem(int position) { 
    switch (position) { 
    case 0: 
     Intent i1 = new Intent(MainActivity.this, setSelfTimeActivity.class); 
     this.startActivity(i1); 
     break; 
    } 

} 

@Override 
public void setTitle(CharSequence title) { 
    mTitle = title; 
    getActionBar().setTitle(mTitle); 
} 

@Override 
protected void onPostCreate(Bundle savedInstanceState) { 
    super.onPostCreate(savedInstanceState); 
    // Sync the toggle state after onRestoreInstanceState has occurred. 
    mDrawerToggle.syncState(); 
} 

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 
    mDrawerToggle.onConfigurationChanged(newConfig); 
} 

public boolean onOptionsItemSelected(MenuItem item) { 
    // Pass the event to ActionBarDrawerToggle, if it returns 
    // true, then it has handled the app icon touch event 
    if (mDrawerToggle.onOptionsItemSelected(item)) { 
     return true; 
    } 
    // Handle your other action bar items... 

    return super.onOptionsItemSelected(item); 
} 
} 

我認爲這個問題可能與這個方法有關,因爲我看到的每個地方都有關於selectItem方法的評論。我沒有看到任何錯誤,但。

/** Swaps fragments in the main content view */ 
/** Starts an Activity when item is clicked*/ 
private void selectItem(int position) { 
    switch (position) { 
    case 0: 
     Intent i1 = new Intent(MainActivity.this, setSelfTimeActivity.class); 
     startActivity(i1); 
     break; 
    } 

} 
+0

這絕對是可以做到的,我自己也多次做了這個,並且剛剛回答了一個類似的問題。請參閱http://stackoverflow.com/questions/27312813/multiple-activities-not-fragments-with-a-navigationdrawer-how-to-show-current/27314192#27314192 – Chris 2014-12-05 11:21:46

+0

[相同的導航抽屜在不同的活動](http://stackoverflow.com/questions/19451715/same-navigation-drawer-on-different-activities) – Chris 2014-12-05 11:23:23

回答