首页 > 其他 > 详细

Navigation drawer 和 Up Carat在fragments中的转换

时间:2014-03-11 19:54:01      阅读:521      评论:0      收藏:0      [点我收藏+]

问题:在使用抽屉的情况下,往下层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);
		


Navigation drawer 和 Up Carat在fragments中的转换,布布扣,bubuko.com

Navigation drawer 和 Up Carat在fragments中的转换

原文:http://blog.csdn.net/zqx198810/article/details/21009037

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!