FragmentStatePagerAdapter not calling getItem
Asked Answered
W

1

6

The problem: I have a ViewPager setup with a FragmentStatePagerAdapter that uses a global arraylist for the contents of its fragments. When I want to update this global array, I simply call the arraylist.add() method, instantiate a new FragmentStatePagerAdapter and a new ViewPager. However, for some reason, the Adapter isn't calling getItem at all. Not that it's data set is empty (it isn't) or anything... it simply isn't being called. I even set a log to test it. Here's my code:

// Create the adapter that will return a fragment for event
SectionsPagerAdapter adapter = new SectionsPagerAdapter(getFragmentManager());

// Set up the ViewPager with the sections adapter.
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);

/**
 * A FragmentStatePagerAdapter that returns a fragment corresponding to
 * an index of the global events array
 */
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

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

    /**
     * Called to instantiate the fragment for the given page
     */
    @Override
    public Fragment getItem(int position) {
        Log.v("Miles", "getItem called");
        // getItem is called to instantiate the fragment for the given page.
        return EventFragment.newInstance(events.get(position));
    }

    /**
     * Total number of pages (fragments) there are
     * Given by size of the global events array minus 1
     */
    @Override
    public int getCount() {
        return events.size() - 1;
    }
}

Are there any problems I could have with this setup? I can verify that the global arraylist has data inside of it, but no fragment is being instantiated to reflect that.

Whitford answered 25/7, 2014 at 14:41 Comment(5)
You don't need to create a new adapter. Add the item to your list and call notifyDataSetChanged on the existing adapter. getItem is only called when the ViewPager needs the next page. It will not be called right away when you change the adapter data.Oratorio
Right, so when I add the item to the arraylist, then go back to the activity holding the viewpager, nothing is displayed. As in, the item I just added to the arraylist is not there. I confirmed that there are no new fragments being added to the adapter by calling 'getCount()'.Whitford
And you are calling notifyDataSetChanged?Oratorio
I am, yeah. Sorry for the late response, was in a meeting.Whitford
So I fixed the problem by making my adapter a global variable. I was hesitant to do this because I think it looks ugly, but it works. Thanks for your help.Whitford
C
0

You can try to override getItemPosition

override fun getItemPosition(object: Any): Int { return POSITION_NONE }

Crude answered 26/10, 2022 at 8:27 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewUnoccupied

© 2022 - 2024 — McMap. All rights reserved.