使用片段时,Android的导航抽屉的图像和向上插入符号之间切换抽屉、片段、符号、图像

2023-09-12 00:15:03 作者:深情的rapper.

在使用导航抽屉在Android开发者的建议,在动作条只有那些重新psented在导航抽屉$ P $屏幕实际上应该有导航抽屉形象,所有其他的屏幕具有传统的向上克拉。

在这里看到的细节: http://youtu.be/F5COhlbpIbY

我用一个活动来控制多层次的片段,并能得到导航抽屉图像,各级显示和功能。

在创建较低级别的片段,我可以叫 ActionBarDrawerToggle setDrawerIndicatorEnabled(假)隐藏抽屉式导航图像和已经显示向上插入符

  LowerLevelFragment lowFrag =新LowerLevelFragment();

//禁用切换菜单和显示克拉
theDrawerToggle.setDrawerIndicatorEnabled(假);
getSupportFragmentManager()的BeginTransaction()代替(R.id.frag_layout,
lowFrag,lowerFrag)addToBackStack(空).commit()。
 
豆瓣FM 4.0 Android版本发布,加入主动搜索的功能

我遇到的问题是,当我浏览回到顶层片段向上克拉仍显示代替了原来的导航抽屉的图像。如何刷新的动作条上的顶级片段重新显示导航抽屉图像有什么建议?

解决方案

汤姆的建议为我工作。这是我做的:

MainActivity

本活动控制在应用程序中的所有片段。

在preparing新的片段,以取代别人,我设置了DrawerToggle setDrawerIndicatorEnabled(假)是这样的:

  LowerLevelFragment lowFrag =新LowerLevelFragment();

//禁用切换菜单和显示克拉
theDrawerToggle.setDrawerIndicatorEnabled(假);
getSupportFragmentManager()的BeginTransaction()代替(R.id.frag_layout,
lowFrag).addToBackStack(空).commit();
 

接下来,在 onBack pressed 的重写,我恢复了上面的DrawerToggle设置为 setDrawerIndicatorEnabled(真)是这样的:

  @覆盖
公共无效onBack pressed(){
    super.onBack pressed();
    //打开抽屉式导航图像;
    //这就是所谓的LowerLevelFragments
    setDrawerIndicatorEnabled(真)
}
 

在LowerLevelFragments

在我修改的片段的onCreate onOptionsItemSelected 是这样的:

的onCreate 添加 setHasOptionsMenu(真)启用配置选项菜单。同时设定 setDisplayHomeAsUpEnabled(真)启用< 在动作条:

  @覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    //需要指出片段将
    //喜欢的项目添加到选项菜单
    setHasOptionsMenu(真正的);
    //更新动作条显示了克拉/启示
    。getActivity()getActionBar()setDisplayHomeAsUpEnabled(真)。
}
 

然后在 onOptionsItemSelected 只要在< 是pressed它调用 onBack pressed( )从活动到上一级的层次结构,并显示导航抽屉图片:

  @覆盖
公共布尔onOptionsItemSelected(菜单项项){
    //获取项目选择和处理它
    开关(item.getItemId()){
        案例android.R.id.home:
            //调用的时候了启示/克拉的动作条为pressed
            getActivity()onBack pressed()。
            返回true;
        ...
    }
 

解决方案

您已经撰文指出,实现低级别的碎片,你要替换现有的片段,而不是执行较低级别的片段在一个新的活动。

我会认为你将不得不手动执行后面的功能:当用户pressed回你有code弹出堆栈(如活动:: onBack pressed 覆盖)。所以,无论你做,你可以扭转 setDrawerIndicatorEnabled

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."

See here for details: http://youtu.be/F5COhlbpIbY

I'm using one activity to control multiple levels of fragments and can get the Navigation Drawer image to display and function at all levels.

When creating lower level fragments I can call the ActionBarDrawerToggle setDrawerIndicatorEnabled(false) to hide the Navigation Drawer image and have the Up caret displayed

LowerLevelFragment lowFrag = new LowerLevelFragment();

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

The problem I'm having is when I navigate back to the top level fragments the Up carat still shows instead of the original Navigation Drawer image. Any suggestions on how to "refresh" the ActionBar on the top level fragments to re-display the Navigation Drawer image?

Solution

Tom's suggestion worked for me. Here’s what I did:

MainActivity

This activity controls all fragments in the app.

When preparing new fragments to replace others, I set the DrawerToggle setDrawerIndicatorEnabled(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 to setDrawerIndicatorEnabled(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 set setDisplayHomeAsUpEnabled(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;
        … 
    }

解决方案

You have written that, to implement lower-level fragments, you are replacing the existing fragment, as opposed to implementing the lower-level fragment in a new activity.

I would think that you would then have to implement the back functionality manually: when the user pressed back you have code that pops the stack (e.g. in Activity::onBackPressed override). So, wherever you do that, you can reverse the setDrawerIndicatorEnabled.