不幸的是,当我尝试在标签之间切换时,我收到类似于此人(FragmentTransaction.replace() not working)的错误,其中第一个标签中的文本列表出现在每个其他片段的背景中。我确实按照建议将FrameLayout与容器一起使用,这样我就可以在切换选项卡时添加或删除相关的片段(这也是Fragment duplication on Fragment Transaction推荐的)。我试图使用来自Switching fragments within tab的@Niek的TabListener解决方案,但问题仍然存在。
非常感谢所有帮助,非常感谢您的时间。
来自我的MainActivity:
//Making an Action Bar
ActionBar actionbar = getSupportActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.setTitle("MyApp");
//Creating the Tabs
ActionBar.Tab Frag1Tab = actionbar.newTab().setText("BlackList");
ActionBar.Tab Frag2Tab = actionbar.newTab().setText("TreeMenu");
ActionBar.Tab Frag3Tab = actionbar.newTab().setText("Map");
ActionBar.Tab Frag4Tab = actionbar.newTab().setText("Help");
//Fragments (Underlying Classes for Each Class)
Fragment1 = new BlackistFragment();
Fragment2 = new TreeMenuFragment();
Fragment3 = new Fragment_3();
Fragment4 = new Fragment_4();
//Adding Tab Listeners
//new TabListener<StationsFragment>(this, "stations", StationsFragment.class)
Frag1Tab.setTabListener(new TabListener<BlackistFragment>(this, "frag1", BlackistFragment.class));
Frag2Tab.setTabListener(new TabListener<TreeMenuFragment>(this, "frag2", TreeMenuFragment.class));
Frag3Tab.setTabListener(new TabListener<Fragment_3>(this, "frag3", Fragment_3.class));
Frag4Tab.setTabListener(new TabListener<Fragment_4>(this, "frag4", Fragment_4.class));
//Adding Tabs to Action Bar
actionbar.addTab(Frag1Tab);
actionbar.addTab(Frag2Tab);
actionbar.addTab(Frag3Tab);
actionbar.addTab(Frag4Tab);
标签监听器......
public class TabListener<T extends SherlockFragment> implements com.actionbarsherlock.app.ActionBar.TabListener {
private final SherlockFragmentActivity mActivity;
private final String mTag;
private final Class<T> mClass;
private SherlockFragment mFragment;
public TabListener(SherlockFragmentActivity activity, String tag, Class<T> class1) {
mActivity = activity;
mTag = tag;
mClass = class1;
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
SherlockFragment preInitializedFragment = (SherlockFragment) mActivity.getSupportFragmentManager().findFragmentByTag(mTag);
if (preInitializedFragment == null) {
mFragment = (SherlockFragment) SherlockFragment.instantiate(mActivity, mClass.getName());
ft.add(R.id.fragment_container, mFragment, mTag);
} else {
ft.attach(preInitializedFragment);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
SherlockFragment preInitializedFragment = (SherlockFragment) mActivity.getSupportFragmentManager().findFragmentByTag(mTag);
if (preInitializedFragment != null) {
ft.detach(preInitializedFragment);
} else if (mFragment != null) {
ft.detach(mFragment);
}
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// User selected the already selected tab. Usually do nothing.
}
}
我的BlackistFragment标签中的onCreateView(显示在所有其他标签中的那个..)。请注意,它扩展了SherlockFragment
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
view = inflater.inflate(R.layout.blacklist, container, false);
ListView listView = (ListView) view.findViewById(R.id.listview);
//Making BlackList
TreeSet<BlacklistWord> theSet = MainActivity.getInstance().datasource.GetAllWords();
ArrayList<String> list = new ArrayList<String>();
for(BlacklistWord i :theSet){
System.out.println(i.getWord());
list.add(i.getWord());
}
Collections.sort(list);
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
((BaseAdapter) listView.getAdapter()).notifyDataSetChanged();
((ViewGroup) listView.getParent()).removeView(listView);
container.addView(listView);
return view;
}
我的第二个片段课......
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
MainActivity.getInstance().invalidateOptionsMenu();
view = inflater.inflate(R.layout.treemenu, container, false);
//Auto-Complete
AutoCompleteTextView autoView = (AutoCompleteTextView) view.findViewById(R.id.edit_message);
String[] itemOptions = getResources().getStringArray(R.array.edit_message);
ArrayAdapter<String> theAdapter =
new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, itemOptions);
autoView.setAdapter(theAdapter);
((BaseAdapter) autoView.getAdapter()).notifyDataSetChanged();
// ((ViewGroup) autoView.getParent()).removeView(autoView);
Button b = (Button) view.findViewById(R.id.post_blacklist_button);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AutoCompleteTextView editText=(AutoCompleteTextView) view.findViewById(R.id.edit_message);
String blackListItem = editText.getText().toString();
editText.setText("");
MainActivity.getInstance().postBlackListItem(blackListItem);
//refreshes the rest
MainActivity.getInstance().invalidateOptionsMenu();
}
});
initViews(view);
if(view != null) { return view; }
((ViewGroup) autoView.getParent()).removeView(autoView);
container.addView(autoView);
MainActivity.getInstance().invalidateOptionsMenu();
return view;
}
答案 0 :(得分:1)
我认为向您展示如何实现它比修改代码更容易。然后,您可以轻松地根据您的使用情况进行调整。
编辑:我更改了一些变量以匹配您的程序。
这是实现TabListener的类:
public class CustomTabListener implements TabListener {
private Fragment fragment;
public CustomTabListener(Fragment frag){
fragment = frag;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
//Tab Reselected....
}
}
然后你可以像这样添加标签......
private void addTabs() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setTitle("MyApp");
actionBar.addTab(actionBar.newTab()
.setText("BlackList")
.setTabListener(new CustomTabListener(Fragment1)));
actionBar.addTab(actionBar.newTab()
.setText("TreeMenu")
.setTabListener(new CustomTabListener(Fragment2)));
actionBar.addTab(actionBar.newTab()
.setText("Map")
.setTabListener(new CustomTabListener(Fragment3)));
actionBar.addTab(actionBar.newTab()
.setText("Help")
.setTabListener(new CustomTabListener(Fragment4)));
}
你所要做的就是将片段传递给它。