0

I'm on the verge of losing it. Anyone that could help me solve this issue will be showered with coding props for all of eternity.

I have a Tablayout containing 3 Tabs within my Main Activity. Each tab contains their own Fragment(Tasks,Calendar,Contacts). There is a FAB button in the Main Activity that lives on top of all the Fragments and performs a certain action base on what Tab I'm currently in.

My main issue is that, when I'm in Tab1, and the Fab is clicked, I need to replace the Tasks Fragment with a different Fragment called AddContact.

I've been looking up how to solve this for hours and finally settled on an earlier answer from this stack post

The issue is, I'm not quite grasping how they were able to do it.

My understanding is that the swapping of Fragments should be managed by the getItem method within the View Pager Adapter. And that there should be a listener that exists between the ViewPagerAdapter and the buttonClicked action in my Main Activity that tells it to execute.

The problem is that I can't make this link. I've tried a number of things but with no luck. Below I included my MainActivity Class and my ViewPagerAdapter Class. If someone could please explain to me how I could swap out my Tasks Fragment with my AddContacts Fragment in the ViewPager, I would greatly appreciate it

Main Activity:

public class MainActivity extends AppCompatActivity {
private CollapsingToolbarLayout collapsingToolbarLayout;
private ViewPager viewPager;
private ArrayList<Fragment> fragments = new ArrayList<>();
private ArrayList<String> titles = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    collapsingToolbarLayout = (CollapsingToolbarLayout)findViewById(R.id.collapsing_toolbar);
    setSupportActionBar(toolbar);

    fragments.addAll(Arrays.asList(new Tasks(),new Calendar(),new Contacts()));
    titles.addAll(Arrays.asList(getResources().getString(R.string.fragment_task_title),
            getResources().getString(R.string.fragment_cal_title),getResources().getString(R.string.fragment_contacts_title)));
    TabLayout tabLayout = (TabLayout)findViewById(R.id.tabs);
    final FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);

    viewPager = (ViewPager)findViewById(R.id.container);
    ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(),fragments,titles);
    viewPager.setAdapter(viewPagerAdapter);
    viewPager.setCurrentItem(1);

    tabLayout.setupWithViewPager(viewPager);

    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            fabAction();
        }
    });

}

public void fabAction(){
    int current = viewPager.getCurrentItem();

    switch (current){
        case 0:{
            //Need to indicate Fragment Swap here somehow. Through Listener?
            break;
        }

    }
}}

ViewPagerAdapter Class:

public class ViewPagerAdapter extends FragmentPagerAdapter {
ArrayList<Fragment> fragments;
ArrayList<String> titles;
private Fragment mFragmentAtPos0;

public ViewPagerAdapter(FragmentManager fm, ArrayList<Fragment> fragments, ArrayList<String>titles) {
    super(fm);
    this.fragments = fragments;
    this.titles = titles;
}

@Override
public Fragment getItem(int position) {
    if (position == 0)
    {
        if (mFragmentAtPos0 == null)
        {
            //HANDLES THE CHANGING OF FRAGMENTS IN HERE USING THE LISTENER BELOW? BUT I'M NOT GRASPING THE LOGIC
            //ON HOW TO IMPLEMENT THIS.
        }
    }

    return fragments.get(position);
}

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

@Override
public CharSequence getPageTitle(int position) {

    return titles.get(position);
}

//GOT FROM LESSON - ALLOWS VIEWPAGER TO CHANGE IT'S VIEW AND ATTACH NEW FRAGMENT WHEN LAUNCHED
@Override
public int getItemPosition(Object object)
{
    if (object instanceof Tasks && mFragmentAtPos0 instanceof AddContact)
        return POSITION_NONE;
    return POSITION_UNCHANGED;
}

//GOT FROM LESSON. A LISTENER BUT I'M NOT SURE WHERE AND HOW TO IMPLEMENT IT. WHERE IN THE MAIN ACTIVITY SHOULD THIS
//GO? HOW DOES IT COMMUNICATE BACK TO THE ADAPTER?
public interface FirstPageFragmentListener
{
    void onSwitchToNextFragment();
}

The post or lesson that Im referring is also from 5 years ago so I don't know if there's a simpler way to implement this, in which case I would love to hear it. Thanks so much.

Community
  • 1
  • 1
Mark F
  • 1,433
  • 3
  • 19
  • 40
  • From your description it does not sound like you are actually changing fragments, sounds more like you are just updating the data. If that is the case, update your data appropriately and call notifyDataSetChanged() – portfoliobuilder Mar 13 '17 at 22:10
  • 1
    Neither `FragmentPagerAdapter` nor `FragmentStatePagerAdapter` handle this sort of scenario well. I recommend either not changing the type of fragment in the tab, [switching to a horizontal `RecyclerView` and `LinearSnapHelper`](https://github.com/commonsguy/cw-omnibus/tree/master/RecyclerViewPager/DynSnap), or using [my `ArrayPagerAdapter`](https://github.com/commonsguy/cwac-pager). – CommonsWare Mar 13 '17 at 22:16

0 Answers0