diff options
| author | Bananeweizen <bananeweizen@gmx.de> | 2013-12-08 10:45:34 +0100 |
|---|---|---|
| committer | Bananeweizen <bananeweizen@gmx.de> | 2013-12-08 10:45:34 +0100 |
| commit | 9d4929006421bf225e0f564ad25d1cf78aa63b06 (patch) | |
| tree | 4e871a509bee9166019550a5c3410f84994aa44f | |
| parent | 1e12d6a1396b208614046cf00e88d008b3e08cba (diff) | |
| download | cgeo-9d4929006421bf225e0f564ad25d1cf78aa63b06.zip cgeo-9d4929006421bf225e0f564ad25d1cf78aa63b06.tar.gz cgeo-9d4929006421bf225e0f564ad25d1cf78aa63b06.tar.bz2 | |
refactor: generalize state storing for listview based viewpager pages
4 files changed, 60 insertions, 39 deletions
diff --git a/main/src/cgeo/geocaching/activity/AbstractViewPagerActivity.java b/main/src/cgeo/geocaching/activity/AbstractViewPagerActivity.java index e6167fb..bb0273f 100644 --- a/main/src/cgeo/geocaching/activity/AbstractViewPagerActivity.java +++ b/main/src/cgeo/geocaching/activity/AbstractViewPagerActivity.java @@ -113,8 +113,10 @@ public abstract class AbstractViewPagerActivity<Page extends Enum<Page>> extends // Store the state of the view if the page supports it PageViewCreator creator = viewCreators.get(page); - Bundle state = creator.getViewState(); - viewStates.put(page, state); + if (creator != null) { + Bundle state = creator.getViewState(); + viewStates.put(page, state); + } container.removeView((View) object); } diff --git a/main/src/cgeo/geocaching/ui/AbstractCachingListViewPageViewCreator.java b/main/src/cgeo/geocaching/ui/AbstractCachingListViewPageViewCreator.java new file mode 100644 index 0000000..8588e21 --- /dev/null +++ b/main/src/cgeo/geocaching/ui/AbstractCachingListViewPageViewCreator.java @@ -0,0 +1,53 @@ +package cgeo.geocaching.ui; + +import cgeo.geocaching.activity.AbstractViewPagerActivity.PageViewCreator; + +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 Bundle getViewState() { + if (view == null) { + return null; + } + int position = view.getFirstVisiblePosition(); + View child = view.getChildAt(0); + int positionFromTop = (child == null) ? 0 : child.getTop(); + 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(Bundle state) { + if (view == null) { + return; + } + int logViewPosition = state.getInt(STATE_POSITION); + int logViewPositionFromTop = state.getInt(STATE_POSITION_FROM_TOP); + view.setSelectionFromTop(logViewPosition, logViewPositionFromTop); + return; + } + +} diff --git a/main/src/cgeo/geocaching/ui/logs/CacheLogsViewCreator.java b/main/src/cgeo/geocaching/ui/logs/CacheLogsViewCreator.java index 0a327f3..6311476 100644 --- a/main/src/cgeo/geocaching/ui/logs/CacheLogsViewCreator.java +++ b/main/src/cgeo/geocaching/ui/logs/CacheLogsViewCreator.java @@ -1,17 +1,16 @@ package cgeo.geocaching.ui.logs; import cgeo.geocaching.CacheDetailActivity; +import cgeo.geocaching.CgeoApplication; import cgeo.geocaching.Geocache; import cgeo.geocaching.LogEntry; import cgeo.geocaching.R; -import cgeo.geocaching.CgeoApplication; import cgeo.geocaching.enumerations.LogType; import cgeo.geocaching.ui.UserActionsClickListener; import org.apache.commons.lang3.StringUtils; import android.content.res.Resources; -import android.os.Bundle; import android.view.View; import android.widget.TextView; @@ -110,37 +109,4 @@ public class CacheLogsViewCreator extends LogsViewCreator { return new UserActionsClickListener(getCache()); } - /** - * Get the state of the current view - * - * @return the state encapsulated in a bundle - */ - @Override - public Bundle getViewState() { - if (view == null) { - return null; - } - int position = view.getFirstVisiblePosition(); - View child = view.getChildAt(0); - int positionFromTop = (child == null) ? 0 : child.getTop(); - Bundle state = new Bundle(); - state.putInt("position", position); - state.putInt("positionFromTop", positionFromTop); - return state; - } - - /** - * Restore a previously stored state of the view - * - */ - @Override - public void setViewState(Bundle state) { - if (view == null) { - return; - } - int logViewPosition = state.getInt("position"); - int logViewPositionFromTop = state.getInt("positionFromTop"); - view.setSelectionFromTop(logViewPosition, logViewPositionFromTop); - return; - } } diff --git a/main/src/cgeo/geocaching/ui/logs/LogsViewCreator.java b/main/src/cgeo/geocaching/ui/logs/LogsViewCreator.java index 15634d3..fb72ee5 100644 --- a/main/src/cgeo/geocaching/ui/logs/LogsViewCreator.java +++ b/main/src/cgeo/geocaching/ui/logs/LogsViewCreator.java @@ -8,7 +8,7 @@ import cgeo.geocaching.activity.AbstractActivity; import cgeo.geocaching.activity.Progress; import cgeo.geocaching.list.StoredList; import cgeo.geocaching.network.HtmlImage; -import cgeo.geocaching.ui.AbstractCachingPageViewCreator; +import cgeo.geocaching.ui.AbstractCachingListViewPageViewCreator; import cgeo.geocaching.ui.AnchorAwareLinkMovementMethod; import cgeo.geocaching.ui.DecryptTextClickListener; import cgeo.geocaching.ui.Formatter; @@ -30,7 +30,7 @@ import android.widget.TextView; import java.util.ArrayList; import java.util.List; -public abstract class LogsViewCreator extends AbstractCachingPageViewCreator<ListView> { +public abstract class LogsViewCreator extends AbstractCachingListViewPageViewCreator { protected final AbstractActivity activity; |
