如何更改特定片段中的NavDrawer图标

问题描述:

那么,在我的应用程序中,我有一个负责显示新闻调用的片段,该片段也是我的导航抽屉中的一个项目。如何更改特定片段中的NavDrawer图标

用户可以点击新闻电话,然后输入一个新的片段。

我想要的是,当用户输入这个新的片段,其中有新闻,NavDrawer之前的图标,转动一个返回箭头。

这是存放的新闻呼叫 This is the fragment that holds the calls to the news

这是记者片段,它应该有返回箭头 This is the news fragment, which should have the return arrow

你可以做这样的片段

in fragment override onCreate method like this

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setHasOptionsMenu(true); // note this line 
    } 

然后覆盖onActivityCreated方法这样

@Override 
    public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     ((MainActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true); // this is your back arrow 

    } 

,并在最后一个向后的箭头覆盖的点击onOptionsItemSelected方法

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     int id = item.getItemId(); 

     switch (id) { 
      case android.R.id.home: 

       if (getActivity() != null) 
        getActivity().onBackPressed(); 

       break; 
     } 
     return super.onOptionsItemSelected(item); 

    } 

完成!希望这是你想要的..

+0

这不适合我,后箭头不出现 –