Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment Replace With ViewPager in ActionBar Tab

I have ActionBar tabs with view pager feature. My problem is in these tabs, I can not replace fragments. I use the code below to replace fragments but it does not replace, the main fragment is remained. In addition, in other tabs also the second fragment exists. My TabListener is also below the replacement of fragments.

private void addFragment(Fragment fragment, boolean addToBackStack, int transition)
{
    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.details_container, fragment);
    fragmentTransaction.setTransition(transition);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}

public static class TabsAdapter extends FragmentPagerAdapter
        implements ViewPager.OnPageChangeListener, ActionBar.TabListener {
    private final Context mContext;
    private final ActionBar mBar;
    private final ViewPager mViewPager;
    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

    static final class TabInfo {
        private final Class<?> clss;
        private final Bundle args;

        TabInfo(Class<?> _class, Bundle _args) {
            clss = _class;
            args = _args;
        }
    }

    public TabsAdapter(FragmentActivity activity, ActionBar bar, ViewPager pager) {
        super(activity.getSupportFragmentManager());
        mContext = activity;
        mBar = bar;
        mViewPager = pager;
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);
    }

    public void addTab(ActionBar.Tab tab, Class<? extends Fragment> clss, Bundle args) {
        TabInfo info = new TabInfo(clss, args);
        tab.setTag(info);
        tab.setTabListener(this);
        mTabs.add(info);
        mBar.addTab(tab);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mTabs.size();
    }

    @Override
    public Fragment getItem(int position) {
        TabInfo info = mTabs.get(position);
        return Fragment.instantiate(mContext, info.clss.getName(), info.args);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        mBar.setSelectedNavigationItem(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        Object tag = tab.getTag();
        for (int i=0; i<mTabs.size(); i++) {
            if (mTabs.get(i) == tag) {
                mViewPager.setCurrentItem(i);
            }
        }
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {

    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {

    }
}
like image 542
adaclks Avatar asked Nov 13 '22 02:11

adaclks


1 Answers

I'd suggest you look through the whole Replace Fragment inside a ViewPager topic and check out example in https://stackoverflow.com/a/11974777/685292 .

like image 112
Blackhex Avatar answered Jan 12 '23 05:01

Blackhex