blob: 40d077e1c11f4e05150486fc0b693dc7a07fb470 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
package cgeo.geocaching.ui;
import cgeo.geocaching.activity.AbstractViewPagerActivity.PageViewCreator;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ListView;
/**
* {@link PageViewCreator} for {@link ListView}, which can save scroll state on purging a page from the
* {@link ViewPager}, and restore the state on re-recreation.
*
*/
public abstract class AbstractCachingListViewPageViewCreator extends AbstractCachingPageViewCreator<ListView> {
private static final String STATE_POSITION_FROM_TOP = "positionFromTop";
private static final String STATE_POSITION = "position";
/**
* Get the state of the current view
*
* @return the state encapsulated in a bundle
*/
@Override
public @Nullable
Bundle getViewState() {
if (view == null) {
return null;
}
final int position = view.getFirstVisiblePosition();
final View child = view.getChildAt(0);
final int positionFromTop = (child == null) ? 0 : child.getTop();
final Bundle state = new Bundle();
state.putInt(STATE_POSITION, position);
state.putInt(STATE_POSITION_FROM_TOP, positionFromTop);
return state;
}
/**
* Restore a previously stored state of the view
*
*/
@Override
public void setViewState(@NonNull final Bundle state) {
if (view == null) {
return;
}
final int logViewPosition = state.getInt(STATE_POSITION);
final int logViewPositionFromTop = state.getInt(STATE_POSITION_FROM_TOP);
view.setSelectionFromTop(logViewPosition, logViewPositionFromTop);
}
}
|