Navigation drawer 和 Up Carat在fragments中的轉換

問題:在使用抽屜的情況下,往下層fragment導航,其中某一個下層的fragment需要網上導航,即在這個fragment中,按壓logo不希望出現抽屜而是希望出現向上導航。

When using the Navigation Drawer the Android devs are recommending that in the ActionBar "only those screens that are represented in the Navigation Drawer should actually have the Navigation Drawer image" and that "all other screens have the traditional up carat."

解決:

private ActionBarDrawerToggle mDrawerToggle;
mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
		mDrawerLayout, /* 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) {

				Log.e(TAG, "onDrawerClosed");

				super.onDrawerClosed(view);

				if (currentDrawerPosition == 0) // 不同的界面顯示不同的title
					actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

				getActionBar().setTitle(mTitle);

				invalidateOptionsMenu(); // creates call to
											// onPrepareOptionsMenu()
			}

			/** Called when a drawer has settled in a completely open state. */
			public void onDrawerOpened(View drawerView) {

				Log.e(TAG, "onDrawerOpened");

				super.onDrawerOpened(drawerView);
				/**
				 * 打開的時候去掉 tabs,因爲在drawer navigation不能overlay tabs
				 */
				actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);

				getActionBar().setTitle(mDrawerTitle);
				invalidateOptionsMenu(); // creates call to
											// onPrepareOptionsMenu()
			}
		};
這個mDrawerToggle可以指定是否是導航抽屜,如果不是則表明使用向上導航
		//disable the toggle menu and show up carat
		mDrawerToggle.setDrawerIndicatorEnabled(false);

OK 下面是具體的解決辦法:

MainActivity

This activity controls all fragments in the app.

When preparing new fragments to replace others, I set the DrawerTogglesetDrawerIndicatorEnabled(false) like this:


LowerLevelFragment lowFrag = new LowerLevelFragment();

//disable the toggle menu and show up carat
theDrawerToggle.setDrawerIndicatorEnabled(false);
  getSupportFragmentManager().beginTransaction().replace(R.id.frag_layout,   
  lowFrag).addToBackStack(null).commit();

Next, in an override of onBackPressed, I reverted the above by setting the DrawerToggle tosetDrawerIndicatorEnabled(true) like this:

@Override
public void onBackPressed() {
super.onBackPressed();

//turn on the Navigation Drawer image; this is called in the LowerLevelFragments
setDrawerIndicatorEnabled(true)

}

In the LowerLevelFragments

In the fragments I modified onCreate and onOptionsItemSelected like this:

In onCreate added setHasOptionsMenu(true) to enable configuring the options menu. Also setsetDisplayHomeAsUpEnabled(true) to enable the < in the actionbar:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//needed to indicate that the fragment would like to add items to the Options Menu      
setHasOptionsMenu(true);

//update the actionbar to show the up carat/affordance 
getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
}

Then in onOptionsItemSelected whenever the < is pressed it calls the onBackPressed() from the activity to move up one level in the hierarchy and display the Navigation Drawer Image:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

// Get item selected and deal with it
switch (item.getItemId()) {
case android.R.id.home:

//called when the up affordance/carat in actionbar is pressed
getActivity().onBackPressed();
return true;
… 
}

Reference: http://stackoverflow.com/questions/17258020/switching-between-android-navigation-drawer-image-and-up-carat-when-using-fragme

但是當點擊低層fragment的向上導航按鈕時,不會指向之前的界面,如何回到上一個fragment:

因爲上一個fragment是在某個tab中,且低層的這個fragment是沒有tabs於是在低層的fragment中

		actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);  
,按“回退”和“向上導航”都會執行到在tab中的fragment 的resume(),所以在resumes()中加入:

ActionBarActivity activity2 = (ActionBarActivity) getActivity();
		actionBar = activity2.getSupportActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章