我喜欢在活动发生变化时更改菜单。我有MainActivity.java和activity_main.xml菜单。然后我将MyDetailFragment.java与detail_view_menu.xml一起使用。 MainActivity菜单在MainActivity.java的onCreateOptionsMenu中膨胀为
@Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(this).inflate(R.menu.activity_main, menu);
return (super.onCreateOptionsMenu(menu));
}
工作正常。当点击其中一个项目并加载MyDetailFragment.java时,我想显示detail_view_menu.xml。我是如何实现的
public class MyDetailsFragment extends Fragment {
TrackerInfo trackerInfo;
public MyDetailsFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.my_details_fragment, container, false);
setHasOptionsMenu(true);
TextView displayText = (TextView) view.findViewById(R.id.details);
displayText.setText(trackerInfo.getIdnumber());
return view;
}
public void retrieveData(TrackerInfo tracker){
trackerInfo = tracker;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.detail_view_menu, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.track:
return (true);
case R.id.view_history:
return (true);
}
return (super.onOptionsItemSelected(item));}}
但问题是菜单仍然是相同的activity_main菜单而且从未改变过。可能是什么问题?
答案 0 :(得分:0)
你需要超级调用相同的方法。
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.detail_view_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
一旦我有问题,新菜单项会附加到菜单中。所以我通过删除所有菜单项来解决它,然后再膨胀新菜单项。 像这样:
public static void emptyFragmentOptionsMenu(Menu menu){
if(menu==null)return;
for(int i=0; i < menu.size(); i++)
menu.removeItem(menu.getItem(i).getItemId());
}
干杯,
编辑:我如何使用该方法:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
emptyFragmentOptionsMenu(menu);
inflater.inflate(R.menu.detail_view_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
答案 1 :(得分:0)
首先:您应该在onCreate()
之上使用onCreateView()
方法并且set setHasOptionsMenu(true);
第二:您需要在super.onCreateOptionsMenu(menu, inflater);
的inflater之后使用onCreateOptionsMenu()
。
第三:您刚刚复制了最初发布的问题 here 。请不要再这样做了。我已经为您提供了有关该问题的其他详细信息,然后您开启了此问题。下次只需编辑原始问题。否则你可能会破坏自己,并为那些试图帮助你的人而烦恼。
执行此操作:
public class MyDetailsFragment extends Fragment {
TrackerInfo trackerInfo;
public MyDetailsFragment() {
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true); // this should go here not in onCreateView();
// ....
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.my_details_fragment, container, false);
TextView displayText = (TextView) view.findViewById(R.id.details);
displayText.setText(trackerInfo.getIdnumber());
return view;
}
public void retrieveData(TrackerInfo tracker){
trackerInfo = tracker;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.detail_view_menu, menu);
super.onCreateOptionsMenu(menu, inflater); // you need to call super
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.track:
return (true);
case R.id.view_history:
return (true);
}
return (super.onOptionsItemSelected(item));
}
}