0

I am trying to replace the fragment views in my tabbed swipe view with another fragment but it does not replace or add the fragment, instead it only adds the menu of the fragment in all the tabbed activity.

I want when in a particular tab activity in my swipeview, I can replace the content(fragmentA) with another fragment(FragmentBoy) which can override that particular menu of that particular tabbed activity. Also could also work when replacing content(fragmentB) with another fragment(FragmentGirl).

Please I stucked, I need a help on this-

public class Main2Activity extends AppCompatActivity {
private ViewPager mViewPager;
private int[] tabIcons = {
        R.drawable.ic_grand,
        R.drawable.ic_event,     
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    mViewPager = (ViewPager) findViewById(R.id.container);
    setupViewPager(mViewPager);
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);
    setupTabIcons();
}
private void setupTabIcons() {
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.getTabAt(0).setIcon(tabIcons[0]);
    tabLayout.getTabAt(1).setIcon(tabIcons[1]);
}
private void setupViewPager(ViewPager mViewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFrag(new FragmentA(), "ONE");
    adapter.addFrag(new FragmentB(), "TWO");    
    mViewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();
    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }
    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }
    @Override
    public int getCount() {
        return mFragmentList.size();
    }
    public void addFrag(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }
    @Override
    public CharSequence getPageTitle(int position) {
        return null;
    }
}
public static class FragmentA extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.grand,container,false);
        Button tb=(Button)view.findViewById(R.id.btn_c);
        tb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentManager manager = getFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction().add(R.id.swapfrag, new BOYFragment());
                transaction.commit();
            }
        });
        return view;
    }
}
Joseph .A.
  • 75
  • 1
  • 9
  • I don't think there is error in code. put error log also if you are getting any error. and one more thing return title from getPageTitle() method. This might be the issue. – Wasim K. Memon May 17 '17 at 04:40
  • @androidnoobdev , ok. I am getting no errors. It doesn't REPLACE the existing tab fragment. I am looking for a way to open another fragment(eg boyfragment) when I click a button from a swipe tabbed activity. The above code does not replace fragmentA with boyFragment, it populates fragmentA over and over again. – Joseph .A. May 17 '17 at 11:16

0 Answers0