Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Fragments Dynamically on both side of the ViewPager using FragmentStatePagerAdapter

In one of my apps, I need to add Fragments on both sides of the ViewPager. First of all, I will get a constant of 5 feeds, and my ViewPager will show feed at index 2 i.e. my current displayed Fragment will contain data present at index 2. So overall my ViewPager will show center of 5 feeds at start and that i have achieved by just setting the ViewPager current Item as 2 like this

viewPager.setCurrentItem(2);

Now user can swipe both sides, when he will swipe left from center position, I will look for next feed i.e fetch from server and add feed at zero index of my ViewPager like this

feedsList.add(0, modelClassObject); // feedsList will contain all feeds and has been used by adapter to show data in fragments.
adapter.notifyDataSetChanged();

and when i swipe right from center position, i will add feed at the last simply like this

feedsList.add(modelClassObject);
adapter.notifyDataSetChanged();

Now the problem is if i only add feeds at the right i.e at the end of the feedsList, everything works fine, but problem comes when i add feeds at zero index. My adapter is not showing that new feed that has been added to zero position instead it is repeating one of the existing feed and that too on the right side but not on the left. I Have tried everything, but nothing is going right way. Here is my adapter code.

private class HorizontalPagerAdapter extends FragmentStatePagerAdapter {

    public HorizontalPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int arg0) {
        return FeedUserProfileFragment.newInstance(feedsList.get(arg0),
                arg0);
    }

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

}

I have also used this

@Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

but no results.. :(

So in severe need, If anyone had done that earlier and faced the same issue, please let me know what i am doing wrong. I only need to add fragment at zero index of my ViewPager.

like image 466
Mukesh Rana Avatar asked Dec 25 '14 07:12

Mukesh Rana


Video Answer


1 Answers

I faced a similar problem before, and my solution was :

  1. at first the list is declared in the adapter itself, so that when creating an instance of that adapter I can have it's own list then.
  2. modified the method getItem(int arg0) in the adapter class so that it returns a specific item from the list depending on that item position.
  3. when creating a new fragment, use instantiate method to create it, and after that add it to your fragments.

So, the complete solution would be :

adapter class:

private class HorizontalPagerAdapter extends FragmentStatePagerAdapter {

public List<Fragment> feedsList;

    public HorizontalPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return feedsList.get(position);
    }

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

}

and when creating the adapter:

public static YourPageAdapter adapter_obj; // make sure it's static object
adapter_obj =  new YourPageAdapter(getSupportFragmentManager());
adapter_obj.feedsList = new ArrayList<Fragment>();
// then add the list of fixed fragments to it(the 5 in the beginning)
adapter_obj.feedsList = fragments_list; // an ArrayList contains the 5 fragments

and when want to create a new fragment:

adapter_obj.feedsList.add(0, Fragment.instantiate(mContext, ViewPager_Container_Class.class.getName(), page));
adapter_obj.notifyDataSetChanged();
like image 170
Muhammed Refaat Avatar answered Oct 29 '22 19:10

Muhammed Refaat