Merge pull request #199 from ryanwarsaw/reset-scroll-github-144

Fix ScrollView not resetting scroll position between fragments, Closes #144
This commit is contained in:
Ryan 2016-09-14 19:16:33 -04:00 коммит произвёл GitHub
Родитель 334475fbe0 8a92383486
Коммит 256c6a7e2a
1 изменённых файлов: 19 добавлений и 1 удалений

Просмотреть файл

@ -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;
}
}
}