UpdatableFragmentStatePagerAdapter

Additional

Language
Java
Version
N/A
Created
Dec 5, 2016
Updated
Apr 14, 2017 (Retired)
Owner
INLOOPX (inloop)
Contributor
DanielNovak
1
Activity
Badge
Generate
Download
Source code
APK file

Blurb

UpdatableFragmentStatePagerAdapter

This is an improved version of the original FragmentStatePagerAdapter with important changes that add support for changing the order and count of items.

Two major problems exist in the FragmentStatePagerAdapter which make it impossible to use with notifyDataSetChanged(), these are desribed as part of our article [Adventures with FragmentStatePagerAdapter](Adventures with FragmentStatePagerAdapter):

You can build and look at the sample implementation.

How to use

The implemenation is the same as with the original FragmentStatePagerAdapter. You need to override two methods in order to support dataset changes:

@Override
public int getItemPosition(Object object) {
    YourFragment fragment = (YourFragment) object;
    // ... determine the position of the fragment in your dataset 
    // ... e.g. extract some ID from Arguments and search in your dataset
}

You need to return POSITION_NONE in case the dataset doesn't contain the item anymore. Always returning POSITION_NONE will work but will have an impact on performance (current Fragments are recreated even if the position hasn't changed).

Also you need to return a unique identifier for an item based on the position. This isually means returning an "ID" or hashcode of the item.

@Override
public long getItemId(int position) {
    // get the item in your dataset on this position and return some ID or hash code.
}

Download

compile 'eu.inloop:pageradapter:0.1.0'