6

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.

3
  • post the code of FeedUserProfileFragment.newInstance(feedsList.get(arg0),arg0);
    – mmlooloo
    Dec 25, 2014 at 9:09
  • I don't think that has any relation with this problem Dec 26, 2014 at 5:17
  • I am also facing the same problem.
    – Neeraj
    Nov 21, 2015 at 11:26

2 Answers 2

4

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();
5
  • Actually I am using only one fragment and everytime data inside that same Fragment will change. So still you think I have to take Fragment ArrayList or instead I should have an ArrayList of data and notify that arrayList everytime when new feed is added ?? Dec 26, 2014 at 8:59
  • I think you have NOT to just add data to ArrayList and notify changes, I believe that won't going so well with fragmenting, take fragment ArrayList or any other way that makes you recreate the fragments from beginning to make sure all your changes will e applied. Dec 26, 2014 at 11:43
  • 1
    I don't know but if that would have been the problem, then i won't be able to add feeds at the end. So it should have worked that way too. Anyways, I have tried yours too, but causes me Exceptions. I have searched for the Exceptions but everything is in vain. So at last i decided to set adapter everytime for the time being. Dec 26, 2014 at 13:34
  • hmmm.. lets see.. awaiting for the right answer. anyways thanks. Your help is really appreciable.. :) Dec 26, 2014 at 14:31
  • 1
    @MuhammedRefaat stackoverflow.com/questions/40149039/… need help
    – albert
    Oct 21, 2016 at 7:08
1

FragmentStatePagerAdapter can't handle it right when you add a new fragment in front.

The workaround is this:

  1. Add a new fragment at the end.
  2. Call notifyDataSetChanged();
  3. Bring the fragment to front.
  4. Call notifyDataSetChanged();

BTW, getItemPosition() should return correct positions all along:

public int getItemPosition(Object object)
{
    return feedsList.indexOf( object );
}

So, with your code, it should be:

newFrag = Fragment.instantiate(...);
feedsList.add( newFrag );
adapter_obj.notifyDataSetChanged();

feedsList.remove( feedsList.size() - 1 );
feedsList.add( 0, newFrag );
adapter_obj.notifyDataSetChanged();

I guess the implementation of FragmentStatePagerAdapter doesn't expect both adding a new fragment and changing position happen at the same time.

1
  • Can you explain why a FragmentPagerAdpter can't handle objects added to the front?
    – Vivek Av
    Feb 16, 2018 at 6:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.