如何在ActionBar选项卡中的单个选项卡中替换Fragment

时间:2014-02-05 07:59:29

标签: android android-fragments android-actionbar android-tabs

我在操作栏标签中有3个标签,每个标签中包含3个不同的片段。我想替换第一个标签中的片段。假设我们目前在 Tab1 中显示 Fragment1 。假设 Fragment1 上有 Listview 。点击列表视图中的项目时,我想将 Fragment1 更改为 Fragment2 。我怎么可能这样做。请帮忙。以下是我的代码:

家长活动代码:

public class ParentActivity extends FragmentActivity {

ActionBar actionBar;
private int mTitleRes;
protected Fragment mFrag;
private CharSequence mTitle;

// TESTING DATAS
private String report_options = "1110000000";
private ReportsTypeChecker reportChecker = new ReportsTypeChecker();

public static class TabListener<T extends Fragment> implements
        ActionBar.TabListener {
    private Fragment mFragment;
    private final FragmentActivity myActivity;
    private final String mTag;
    private final Class<T> mClass;

    /**
     * Constructor used each time a new tab is created.
     * 
     * @param activity
     *            The host Activity, used to instantiate the fragment
     * @param tag
     *            The identifier tag for the fragment
     * @param clz
     *            The fragment's Class, used to instantiate the fragment
     */
    public TabListener(FragmentActivity activity, String tag, Class<T> clz) {
        myActivity = activity;
        mTag = tag;
        mClass = clz;

    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

        System.out.println("ON tabs");
        if (mFragment == null) {
            System.out.println("mFragment null");
            // If not, instantiate and add it to the activity
            mFragment = Fragment.instantiate(myActivity, mClass.getName());
            ft.add(android.R.id.content, mFragment, mTag);

        } else {

            ft.show(mFragment);

        }

    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
        if (mFragment != null) {
            // Detach the fragment, because another one is being attached
            // ft.detach(mFragment);
            ft.hide(mFragment);
        }
    }

}

@Override
public void onCreate(Bundle savedInstanceState) {
    // getWindow().setUiOptions(
    // ActivityInfo.UIOPTION_SPLIT_ACTION_BAR_WHEN_NARROW);
    super.onCreate(savedInstanceState);
    // setContentView(R.layout.parent_layout);
    actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

// HERE IS THE TAB1 THAT CONTAINS THE FRAGMENT1 THAT I WANT TO CHANGE INTO FRAGMENT2

                Tab tab = actionBar
                .newTab()
                .setText(getResources().getString(R.string.parent_tab1))
                .setTabListener(
                        new TabListener<Fragment1>(this,
                                getResources().getString(
                                        R.string.parent_tab1),
                                Fragment1.class));
        actionBar.addTab(tab);

    Tab tab2 = actionBar
            .newTab()
            .setText(getResources().getString(R.string.parent_tab2))
            .setTabListener(
                    new TabListener<AnnouncementsFragment>(this,
                            getResources().getString(R.string.parent_tab1),
                            AnnouncementsFragment.class));
    actionBar.addTab(tab2);

    Tab tab3 = actionBar
            .newTab()
            .setText(getResources().getString(R.string.parent_tab3))
            .setTabListener(
                    new TabListener<TodayFragment>(this, getResources()
                            .getString(R.string.parent_tab3),
                            TodayFragment.class));

    actionBar.addTab(tab3);

}}

这是我的Fragment1:

public class Fragment1 extends Fragment {

ArrayList<String> storeList = new ArrayList<String>();
// String[] storeArray;
protected Fragment mFrag;
Button checkinbtn;
ListView storeLv;
ArrayAdapter<String> arrayAdapter;
String storetitle = "";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View myFragmentView = inflater.inflate(R.layout.fragment_usercheckin,
            container, false);

    // setTitle(getResources().getString(R.string.title_activity_report));

    storeLv = (ListView) myFragmentView.findViewById(R.id.checkin_lv);
    fillStores();

    arrayAdapter = new ArrayAdapter<String>(getActivity(),
            R.layout.listview_textitem, R.id.listview_txt, storeList);
    storeLv.setAdapter(arrayAdapter);

    storeLv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> myAdapter, View myView,
                int myItemInt, long mylng) {

// I want to Replace the current Fragment on the tab1 into Fragment2 here


        }
    });
    return myFragmentView;
}

}

我希望你能帮帮我..谢谢

1 个答案:

答案 0 :(得分:0)

我认为您可以修改TabListener并使用片段列表(或其类)。

public static class TabListener<T extends Fragment> implements
        ActionBar.TabListener {
    private int mIndex;
    private ArrayList<Fragment> mFragments;
    private final FragmentActivity myActivity;
    private final String mTag;
    private final ArrayList<Class<T>> mClasses;

    /**
     * Constructor used each time a new tab is created.
     * 
     * @param activity
     *            The host Activity, used to instantiate the fragment
     * @param tag
     *            The identifier tag for the fragment
     * @param classes
     *            The fragment's Classes, used to instantiate the fragments
     */
    public TabListener(FragmentActivity activity, String tag, ArrayList<Class<T>> classes) {
        myActivity = activity;
        mTag = tag;
        mClasses = classes;
        mIndex = 0;
    }

    public boolean selectNextTab(Tab tab, FragmentTransaction ft) {
        if (mIndex < (mClasses.size() - 1)) {
            mIndex += 1;
            onTabSelected(Tab tab, FragmentTransaction ft);
            return true;
        } else {
            return false;
        }
    }

    public boolean selectPrevTab(Tab tab, FragmentTransaction ft) {
        if (mIndex > 0) {
            mIndex -= 1;
            onTabSelected(Tab tab, FragmentTransaction ft);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
        System.out.println("ON tabs");
        if (mFragments.size() <= mIndex) {
            System.out.println("Fragment doesn't exist yet");
            // If not, instantiate and add it to the activity
            mFragments.add(Fragment.instantiate(myActivity, mClasses.get(mIndex).getName()));
            ft.add(android.R.id.content, mFragments.get(mIndex), mTag);

        } else {
            ft.show(mFragments.get(mIndex));
        }
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
        if (mFragments.get(mIndex) != null) {
            // Detach the fragment, because another one is being attached
            // ft.detach(mFragment);
            ft.hide(mFragments.get(mIndex));
        }
    }
}

然后,您可以通过selectNextTab()访问特定onItemClick()个实例,在TabListener方法中调用ParentActivity方法。

我不确定如何获取TabFragmentTransaction参数实例。也许在第一次调用TabListener时将实例放入onTabSelected()成员变量就足够了。在这种情况下,您的selectNextTab()方法甚至不需要任何参数。

此外,我认为它不适用于通用T定义。您应该可以使用Class<Fragment>

请告诉我这是否有效。祝你好运。