Friday, 13 May 2016

Android Xamarin ViewPager cacheing - unable to renew contents

I just 'wasted' a couple of hours trying to resolve a problem with a ViewPager. I have a fragment that hosts a ViewPager of 7-10 fragments, all of which needed to be swapped out if the user selects a different value/setting. This was working fine in some cases, but when navigating away from the parent fragment and then returning, I seemed unable to get the ViewPager to update properly, showing either old or blank content.

I spent too long stepping through my code trying to work out if I'd missed something, but came up with nothing, and figured that there must be some kind of cacheing going on.

After some reading I learnt that I should be using FragmentStatePagerAdapter and not FragmentPagerAdapter. The difference is explained here.

This, combined with the below override in my adapter, finally solved my problem.
public override int GetItemPosition(Object objectValue)
        {
            return PositionNone;
        }

As ever, I remained indebted to StackOverflow, and this is the answer that resolved it for me.

No comments:

Post a Comment

Comments are moderated, so you'll have to wait a little bit before they appear!