我有一些问题将导航抽屉功能切换到较低级别片段的功能.我已经阅读了
this thread来管理显示指标.但是当按下按钮时,它会打开导航栏,而不是返回到前一个片段.而且我无法将EditUserFragment中的操作栏标题设置为“编辑个人资料”.
我正在使用Android Studio中提供的导航栏模板.
我正在使用Android Studio中提供的导航栏模板.
我有三个层次:
> MainActivity与导航抽屉,由Home和Profile项目组成
> UserFragment标题为“配置文件”,该选项可以显示EditUserFragment
> EditUserFragment(下级片段)由UserFragment触发
主要活动
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); drawerToggle = new ActionBarDrawerToggle(this,drawerLayout,R.drawable.ic_launcher,R.string.navigation_drawer_open,R.string.navigation_drawer_close); } @Override public void onBackPressed() { super.onBackPressed(); // this won't change the drawer indicator back drawerToggle.setDrawerIndicatorEnabled(true); // this works drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); } public void onSectionAttached(int number) { // Show the corresponding title on the action bar when clicked and open corresponding // fragments. Fragment fragment = null; switch (number) { case 1: mTitle = getString(R.string.title_home); break; case 2: mTitle = getString(R.string.title_profile); fragment = new UserFragment(); break; } if (fragment != null) { FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction().replace(R.id.container,fragment).commit(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { if (!mNavigationDrawerFragment.isDrawerOpen()) { // Only show items in the action bar relevant to this screen // if the drawer is not showing. Otherwise,let the drawer // decide what to show in the action bar. getMenuInflater().inflate(R.menu.main,menu); restoreActionBar(); return true; } return super.onCreateOptionsMenu(menu); } public void restoreActionBar() { ActionBar actionBar = getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); actionBar.setTitle(mTitle); } public void setActionBarTitle(String title){ getActionBar().setTitle(title); Log.d("Title 2",getActionBar().getTitle().toString()); }
当我按下手机按钮(操作栏上没有按钮)时,onBackPressed正在部分工作(在代码中注释).
UserFragment
@Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) { setHasOptionsMenu(true); // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_profile,container,false); drawerLayout = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout); drawerToggle = new ActionBarDrawerToggle(getActivity(),R.string.navigation_drawer_close); return view; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); switch (id) { case R.id.action_edit: drawerToggle.setDrawerIndicatorEnabled(false); // Disable sliding from edge to open drawer drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); getFragmentManager().beginTransaction() .replace(R.id.container,new EditUserFragment()) .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE) .addToBackStack(null) .commit(); return true; default: return super.onOptionsItemSelected(item); } }
EditUserFragment
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); getActivity().getActionBar().setDisplayHomeAsUpEnabled(true); ((MainActivity) getActivity()).setActionBarTitle("Edit Profile"); Log.d("Title",getActivity().getActionBar().getTitle().toString()); } @Override public boolean onOptionsItemSelected(MenuItem item) { // Get item selected and deal with it Log.d("KEY: ",String.valueOf(item.getItemId())); switch (item.getItemId()) { case android.R.id.home: Log.d("EditUserFragment","I'm here"); getActivity().onBackPressed(); return true; default: return super.onOptionsItemSelected(item); } }
.getActivity()getActionBar()setDisplayHomeAsUpEnabled(真).按照预期的方式工作,而不是抽屉;虽然点击它,它开放抽屉,而不是回到以前的片段.另外,case android.R.id.home中的代码从不执行.
并且我尝试从“配置文件”将操作栏标题设置为“编辑配置文件”.日志显示我“编辑个人资料”,但由于某些原因,实际运行的应用程序会显示“配置文件”.
我想要实现的是可以通过点击up指示器从EditUserFragment返回到UserFragment(现在up指示器打开导航栏).并在EditUserFragment中显示正确的标题.
任何帮助是极大的赞赏!!
解决方法
经过几天的分析,我发现问题是我在创建时正在使用内置的导航抽屉活动.内置的将任务分成两部分. MainActivity和NavigationDrawerFragment.因此,我在MainActivity中的drawerToggle与NavigationDrawerFragment中的实际不一样.
主角,MainActivty调用NavigationDrawerFragment来设置导航栏所需的所有内容.我通过在MainActivity中实现导航抽屉来解决这个问题,所以我只有一个drawerToggle.虽然如果我在NavigationDrawerFragment中有所有的东西(导航栏的变量),而不是MainActivity,我仍然找不到办法让它工作.如果有人知道答案,请随意留言!