我使用片段,我的一个片段中有一个菜单项。单击列表视图中的项目时,将调用使用该菜单项的片段。当调用带有菜单项的片段,然后我更改选项卡时,菜单项仍然存在。我希望菜单项只显示在一个片段中。
以下是Tab 1的代码
public class Tab2Fragment extends Fragment{
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setHasOptionsMenu(false);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab_fragment, null);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getActivity().setTitle("Tab 2");
initView();
}
private void initView() {
((TextView)getView().findViewById(R.id.tab_text)).setText("Tab 2");
Button button = (Button) getView().findViewById(R.id.tab_btn);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
replaceFragment();
}
});
}
private void replaceFragment() {
((BaseContainerFragment)getParentFragment()).replaceFragment(new Tab2AddOnFragment(), true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
inflater.inflate(R.menu.main, menu);
super.onCreateOptionsMenu(menu, inflater);
}
}
以下是具有列表视图的Tab 3的代码
public class Tab3Fragment extends ListFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setHasOptionsMenu(false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] values = new String[] { "Ang na Ang na", "Sulakihin", "Somaskidot", "Moko Moko"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
getActivity().setTitle("Tab 3");
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
switch(position){
case 0:
// Create new fragment and transaction
FragmentTransaction transaction = getFragmentManager().beginTransaction();
replaceFragment();
transaction.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
// Commit the transaction
transaction.commit();
break;
default:
}
}
private void replaceFragment() {
((BaseContainerFragment)getParentFragment()).replaceFragment(new FragmentInListView(), true);
}
}
以下是带有菜单项
的片段的代码public class FragmentInListView extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
getActivity().setTitle("Fragment Called in Tab 3(List View)");
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.tab_fragment,
container, false);
TextView tv= (TextView) view.findViewById(R.id.tab_text);
tv.setText("Fragment in List View");
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
inflater.inflate(R.menu.travel_tips_expanded, menu);
super.onCreateOptionsMenu(menu, inflater);
}
}
以下是带有菜单项
的片段菜单的代码<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_settings"/>
<item
android:id="@+id/favorite"
android:showAsAction="always"
android:title="favorite"/>
这是在单击列表视图中的项目时调用的Fragment图像。它包含菜单项。
http://s904.photobucket.com/user/fookywooky/media/tab4_zps30944a6e.png.html
这是片段的图像,当&#34; Tab 2&#34;被选中。正如您所看到的,菜单项仍然存在,但我不希望它在那里。此外,即使您单击选项卡1和选项卡3也是如此。菜单项仍然存在。
http://s904.photobucket.com/user/fookywooky/media/tab5_zps285c8314.png.html
请帮忙。提前谢谢。
答案 0 :(得分:0)
为了准确确定何时显示Fragment
菜单,您的Activity
需要一些关于Fragment
可见和最新的指示。您可以使用setMenuVisibility()
和setUserVisibleHint()
方法完成此操作。在OnTabChanged()
回调中,将菜单可见性和用户可见提示设置为先前选定的Fragment
中的false,并将当前选定的Fragment
设置为true。