Skip to content

Commit 8d90238

Browse files
author
Daniel Novak
committed
README
1 parent 62c3225 commit 8d90238

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
UpdatableFragmentStatePagerAdapter
2+
================
3+
4+
This is an improved version of the original [FragmentStatePagerAdapter](https://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html) with important changes that add support for changing the order and count of items.
5+
6+
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):
7+
8+
- State bundles are assigned to wrong Fragments after the dataset order has changed
9+
http://speakman.net.nz/blog/2014/02/20/a-bug-in-and-a-fix-for-the-way-fragmentstatepageradapter-handles-fragment-restoration/
10+
- Crash in case the order of items is changed
11+
http://billynyh.github.io/blog/2014/03/02/fragment-state-pager-adapter/
12+
13+
You can build and look at the [sample implementation](sample/src/main/java/eu/inloop/pager/sample/MainActivity.java).
14+
15+
How to use
16+
--------
17+
18+
The implemenation is the same as with the original FragmentStatePagerAdapter.
19+
You need to override two methods in order to support dataset changes:
20+
21+
```java
22+
@Override
23+
public int getItemPosition(Object object) {
24+
YourFragment fragment = (YourFragment) object;
25+
// ... determine the position of the fragment in your dataset
26+
// ... e.g. extract some ID from Arguments and search in your dataset
27+
}
28+
```
29+
You need to return POSITION_NONE in case the dataset doesn't contain the item anymore.
30+
Always returning POSITION_NONE will work but will have an impact on performance (current Fragments are recreated even if the position hasn't changed).
31+
32+
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.
33+
```java
34+
@Override
35+
public long getItemId(int position) {
36+
// get the item in your dataset on this position and return some ID or hash code.
37+
}
38+
```
39+
40+
Download
41+
--------
42+
43+
```groovy
44+
compile 'eu.inloop:pageradapter:0.1.0'
45+
```

library/src/main/java/eu/inloop/pager/UpdatableFragmentPagerAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public Parcelable saveState() {
181181
}
182182

183183
@Override
184-
public void restoreState(Parcelable state, ClassLoader loader) {
184+
public void restoreState(@Nullable Parcelable state, ClassLoader loader) {
185185
if (state != null) {
186186
Bundle bundle = (Bundle) state;
187187
bundle.setClassLoader(loader);

0 commit comments

Comments
 (0)