From 8a923834860a24576c03802dd60457487c8a7aa2 Mon Sep 17 00:00:00 2001 From: ryanwse Date: Mon, 12 Sep 2016 15:13:06 -0400 Subject: [PATCH] Fix ScrollView not resetting scroll position between fragments, Closes #144 --- .../activities/TutorialSlideActivity.java | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/mozilla/hackathon/kiboko/activities/TutorialSlideActivity.java b/app/src/main/java/com/mozilla/hackathon/kiboko/activities/TutorialSlideActivity.java index 715c73e..31064fd 100644 --- a/app/src/main/java/com/mozilla/hackathon/kiboko/activities/TutorialSlideActivity.java +++ b/app/src/main/java/com/mozilla/hackathon/kiboko/activities/TutorialSlideActivity.java @@ -16,6 +16,7 @@ import android.support.v4.view.ViewPager; import android.support.v7.app.ActionBar; import android.view.MenuItem; import android.view.View; +import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; @@ -90,7 +91,6 @@ public class TutorialSlideActivity extends DSOActivity implements LoaderManager. public void onClick(View v) { if(!(mPager.getCurrentItem() > mPagerAdapter.getCount() - 1)){ mPager.setCurrentItem(mPager.getCurrentItem() - 1); - mNext.setEnabled(true); }else{ mNext.setEnabled(true); @@ -127,6 +127,9 @@ public class TutorialSlideActivity extends DSOActivity implements LoaderManager. // fragment expose actions itself (rather than the activity exposing actions), // but for simplicity, the activity provides the actions in this sample. invalidateOptionsMenu(); + + // When changing pages, get the current page (fragment) and reset the scroll position + ((ScreenSlidePagerAdapter) mPager.getAdapter()).getCurrentFragment().getView().scrollTo(0, 0); } }); @@ -241,6 +244,9 @@ public class TutorialSlideActivity extends DSOActivity implements LoaderManager. * sequence. */ private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter { + + private Fragment mCurrentFragment; + public ScreenSlidePagerAdapter(FragmentManager fm) { super(fm); } @@ -250,9 +256,21 @@ public class TutorialSlideActivity extends DSOActivity implements LoaderManager. return ScreenSlidePageFragment.create(position, jsonSteps.get(position).title, jsonSteps.get(position).description, jsonSteps.get(position).gifUrl); } + @Override + public void setPrimaryItem(ViewGroup container, int position, Object object) { + if (getCurrentFragment() != object) { + mCurrentFragment = (Fragment) object; + } + super.setPrimaryItem(container, position, object); + } + @Override public int getCount() { return jsonSteps.size(); } + + public Fragment getCurrentFragment() { + return mCurrentFragment; + } } }