0

不幸的是,当我尝试在选项卡之间切换时,我收到一个类似于此人的错误(FragmentTransaction.replace() not working),其中我的第一个选项卡中的文本列表出现在所有其他片段的背景中。我确实遵循了将 FrameLayout 与容器一起使用的建议,这样我就可以在切换选项卡时简单地添加或删除相关片段(在 Fragment Transaction 上的 Fragment duplication也被推荐)。我正在尝试使用 @Niek 的 TabListener 解决方案来切换 tab 内的片段,但问题仍然存在。

所有帮助将不胜感激,非常感谢您的宝贵时间。

从我的 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;
}
4

1 回答 1

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)));
}

您所要做的就是将片段传递给它。

于 2013-06-13T05:37:59.630 回答