From 22a2231d2c1f4f4d4197c42b1e33dc6eaba33d65 Mon Sep 17 00:00:00 2001 From: KiwiStone Date: Thu, 28 Nov 2013 01:01:58 +0100 Subject: fixes #3354 - Position not saved on logbook screen --- .../activity/AbstractViewPagerActivity.java | 36 +++++++++++++++++++++- .../ui/AbstractCachingPageViewCreator.java | 19 ++++++++++++ .../geocaching/ui/logs/CacheLogsViewCreator.java | 34 ++++++++++++++++++++ 3 files changed, 88 insertions(+), 1 deletion(-) (limited to 'main/src') diff --git a/main/src/cgeo/geocaching/activity/AbstractViewPagerActivity.java b/main/src/cgeo/geocaching/activity/AbstractViewPagerActivity.java index 952726e..e6167fb 100644 --- a/main/src/cgeo/geocaching/activity/AbstractViewPagerActivity.java +++ b/main/src/cgeo/geocaching/activity/AbstractViewPagerActivity.java @@ -9,6 +9,7 @@ import com.viewpagerindicator.TitleProvider; import org.apache.commons.lang3.tuple.Pair; import android.app.Activity; +import android.os.Bundle; import android.os.Parcelable; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; @@ -43,6 +44,10 @@ public abstract class AbstractViewPagerActivity> extends private final Map viewCreators = new HashMap(); /** + * Store the states of the page views to be able to persist them when destroyed and reinstantiated again + */ + private final Map viewStates = new HashMap(); + /** * The {@link ViewPager} for this activity. */ private ViewPager viewPager; @@ -76,6 +81,16 @@ public abstract class AbstractViewPagerActivity> extends * Handles changed data-sets. */ public void notifyDataSetChanged(); + + /** + * Gets state of the view + */ + public Bundle getViewState(); + + /** + * Set the state of the view + */ + public void setViewState(Bundle state); } /** @@ -93,6 +108,14 @@ public abstract class AbstractViewPagerActivity> extends @Override public void destroyItem(ViewGroup container, int position, Object object) { + + final Page page = pageOrder.get(position); + + // Store the state of the view if the page supports it + PageViewCreator creator = viewCreators.get(page); + Bundle state = creator.getViewState(); + viewStates.put(page, state); + container.removeView((View) object); } @@ -107,6 +130,7 @@ public abstract class AbstractViewPagerActivity> extends @Override public Object instantiateItem(ViewGroup container, int position) { + final Page page = pageOrder.get(position); PageViewCreator creator = viewCreators.get(page); @@ -114,6 +138,7 @@ public abstract class AbstractViewPagerActivity> extends if (null == creator && null != page) { creator = AbstractViewPagerActivity.this.createViewCreator(page); viewCreators.put(page, creator); + viewStates.put(page, new Bundle()); } View view = null; @@ -123,12 +148,16 @@ public abstract class AbstractViewPagerActivity> extends // Result from getView() is maybe cached, but it should be valid because the // creator should be informed about data-changes with notifyDataSetChanged() view = creator.getView(); + + // Restore the state of the view if the page supports it + Bundle state = viewStates.get(page); + creator.setViewState(state); + container.addView(view, 0); } } catch (Exception e) { Log.e("ViewPagerAdapter.instantiateItem ", e); } - return view; } @@ -225,10 +254,15 @@ public abstract class AbstractViewPagerActivity> extends protected abstract String getTitle(Page page); protected final void reinitializeViewPager() { + // notify all creators that the data has changed for (PageViewCreator creator : viewCreators.values()) { creator.notifyDataSetChanged(); } + // reset the stored view states of all pages + for (Bundle state : viewStates.values()) { + state.clear(); + } pageOrder.clear(); final Pair, Integer> pagesAndIndex = getOrderedPages(); diff --git a/main/src/cgeo/geocaching/ui/AbstractCachingPageViewCreator.java b/main/src/cgeo/geocaching/ui/AbstractCachingPageViewCreator.java index 333ef11..55b153b 100644 --- a/main/src/cgeo/geocaching/ui/AbstractCachingPageViewCreator.java +++ b/main/src/cgeo/geocaching/ui/AbstractCachingPageViewCreator.java @@ -2,6 +2,7 @@ package cgeo.geocaching.ui; import cgeo.geocaching.activity.AbstractViewPagerActivity.PageViewCreator; +import android.os.Bundle; import android.view.View; /** @@ -29,4 +30,22 @@ public abstract class AbstractCachingPageViewCreator imp @Override public abstract ViewClass getDispatchedView(); + + /** + * Gets the state of the view but returns an empty state if not overridden + * + * @return empty bundle + */ + @Override + public Bundle getViewState() { + return new Bundle(); + } + + /** + * Restores the state of the view but just returns if not overridden. + */ + @Override + public void setViewState(Bundle state) { + return; + } } diff --git a/main/src/cgeo/geocaching/ui/logs/CacheLogsViewCreator.java b/main/src/cgeo/geocaching/ui/logs/CacheLogsViewCreator.java index 8da711e..873094e 100644 --- a/main/src/cgeo/geocaching/ui/logs/CacheLogsViewCreator.java +++ b/main/src/cgeo/geocaching/ui/logs/CacheLogsViewCreator.java @@ -11,6 +11,7 @@ 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; @@ -109,4 +110,37 @@ 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; + } } \ No newline at end of file -- cgit v1.1