单击时,使用导航滑块替换操作栏向上按钮

时间:2015-04-08 13:17:10

标签: android android-actionbar navigation-drawer

我在我的应用中实施了navigation slider。在这个滑块中,我有四个fragments:主页,个人资料,搜索,计算器。不要担心selectItem方法中的命名约定,因为我现在只关注searchProgram并且没有实现我的所有四个片段 NavigationDrawer.class(Activity):

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

    public void selectItem(int position) {
        Fragment fragment = null;
        switch (position) {
            case 0:
                fragment = new HomePage();
                break;
            case 1:
                fragment = new HomeFragment();
                break;
            case 2:
                fragment = new SearchProgram();
            default:
                break;
        }
        if (fragment != null) {
            Bundle bundle = new Bundle();
            bundle.putInt(HomeFragment.EXTRA_ITEM_INDEX, position);
            bundle.putString("username", userName);
            bundle.putString("access_token", access_token);
            fragment.setArguments(bundle);
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
            mDrawerList.setItemChecked(position, true);
            mDrawerList.setSelection(position);
            setTitle(mNavItems[position]);
            mDrawerLayout.closeDrawer(mDrawerList);
        }

当我选择搜索时,我被带到一个视图,其中action bar具有导航滑块,就像我想要的那样。当用户从两个微调器中选择他们想要搜索的内容并单击我自己的名为搜索的按钮时,然后启动包含可扩展列表的master detail flow。 下面是我从searchProgram类(片段)开始我的新活动的地方:

protected void onPostExecute(ArrayList<SportProgram> programList) {
            Bundle extras = new Bundle();
            String userName = getArguments().getString("username");
            String access_Token = getArguments().getString("accessToken");
            System.out.println("Search Program Test Username: " + userName);
            System.out.println("Search Program Test Access_Token: " + access_Token);
            extras.putString("username", userName);
            extras.putString("accessToken", access_Token);
            extras.putParcelableArrayList("programs", programList);
            Intent intent = new Intent(getActivity(), TrainingProgramListActivity.class);
            intent.putExtras(extras);
            startActivity(intent);
        }
    }

此视图的action bar带有后退/上移按钮,没有导航滑块。当我单击此按钮时,我将返回到上一个视图,即搜索视图,但操作栏保持不变并且仍然具有向上按钮。有没有人知道如何更改操作栏以包含导航滑块像以前一样?当我在listActivity视图中选择后退/上移按钮时,我正在替换当前片段,如下所示:

 @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.menu_list_program, menu);
            return super.onCreateOptionsMenu(menu);
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            int id = item.getItemId();
            Fragment fragment = null;
            switch (item.getItemId()) {
           //This is the back/up button
                case android.R.id.home:
                    fragment = new SearchProgram();
                    break;
                case R.id.action_home:
                    Intent homeIntent = new Intent(this, NavigationDrawer.class);
                    Bundle extras = this.getIntent().getExtras();
                    homeIntent.putExtras(extras);
                    homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(homeIntent);
                    System.out.println("HIT HOMEINTENT");
                    return true;
                case R.id.action_search:
                    fragment = new SearchProgram();
                    System.out.println("HIT SEARCH");
                    break;
                default:
                    break;
            }

            if (fragment != null) {
                Bundle bundle = this.getIntent().getExtras();//new Bundle();
              //  bundle.putString("username", userName);
               // bundle.putString("access_token", access_token);
                fragment.setArguments(bundle);
                FragmentManager fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.containerE, fragment).commit();

             //   fragment.getActivity().supportInvalidateOptionsMenu();

            }
            return super.onOptionsItemSelected(item);
        }

我已经尝试了许多可能的解决方案,但仍然不尽如人意。我找到的所有示例都从navigationDrawer活动导航到片段,然后导航到另一个片段,但对我来说,我正在从NavigationDrawer导航到片段&gt; class-&gt;片段。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您正在将一个详细活动堆叠在包含抽屉的主要活动的顶部。要返回原始活动,只需使用finish()中的onOptionsItemSelected()关闭最后一个活动。