From b1f3c9c8c7ccc72c66fb18caa7dad939846630dd Mon Sep 17 00:00:00 2001 From: Phillip Hsu Date: Sun, 14 Aug 2016 01:23:15 -0700 Subject: [PATCH] Change ArrayList to SparseArray for ViewPager adapter's collection of Fragments --- .../com/philliphsu/clock2/MainActivity.java | 21 +++++++++---------- .../clock2/stopwatch/StopwatchFragment.java | 7 +++++-- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/philliphsu/clock2/MainActivity.java b/app/src/main/java/com/philliphsu/clock2/MainActivity.java index 63146d0..1c3a139 100644 --- a/app/src/main/java/com/philliphsu/clock2/MainActivity.java +++ b/app/src/main/java/com/philliphsu/clock2/MainActivity.java @@ -10,7 +10,7 @@ import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; -import android.util.Log; +import android.util.SparseArray; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; @@ -22,8 +22,6 @@ import com.philliphsu.clock2.settings.SettingsActivity; import com.philliphsu.clock2.stopwatch.StopwatchFragment; import com.philliphsu.clock2.timers.TimersFragment; -import java.util.ArrayList; - import butterknife.Bind; public class MainActivity extends BaseActivity { @@ -52,9 +50,11 @@ public class MainActivity extends BaseActivity { @Bind(R.id.fab) FloatingActionButton mFab; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + // TODO: On device rotation, if we were last on stopwatch page, restore the fab's translationX. // Create the adapter that will return a fragment for each of the three // primary sections of the activity. @@ -276,7 +276,10 @@ public class MainActivity extends BaseActivity { * one of the sections/tabs/pages. */ private static class SectionsPagerAdapter extends FragmentPagerAdapter { - private final ArrayList mFragments = new ArrayList<>(getCount()); + // We can't use an ArrayList because the structure reorganizes as elements are removed, + // so page indices won't stay in sync with list indices. SparseArray allows you to have + // gaps in your range of indices. + private final SparseArray mFragments = new SparseArray<>(getCount()); public SectionsPagerAdapter(FragmentManager fm) { super(fm); @@ -300,15 +303,13 @@ public class MainActivity extends BaseActivity { @Override public Object instantiateItem(ViewGroup container, int position) { Fragment fragment = (Fragment) super.instantiateItem(container, position); - mFragments.add(position, fragment); - Log.d(TAG, "Instantiated fragment " + fragment + " for position " + position + ". New size = " + mFragments.size()); + mFragments.put(position, fragment); return fragment; } @Override public void destroyItem(ViewGroup container, int position, Object object) { - Fragment f = mFragments.remove(position); - Log.d(TAG, "Destroyed fragment " + f + " for position " + position + ". New size = " + mFragments.size()); + mFragments.remove(position); super.destroyItem(container, position, object); } @@ -332,9 +333,7 @@ public class MainActivity extends BaseActivity { } public Fragment getFragment(int position) { - Fragment f = mFragments.get(position); - Log.d(TAG, "Returning fragment " + f + " for position " + position); - return f; + return mFragments.get(position); } } } diff --git a/app/src/main/java/com/philliphsu/clock2/stopwatch/StopwatchFragment.java b/app/src/main/java/com/philliphsu/clock2/stopwatch/StopwatchFragment.java index 3f2f499..9eedd9b 100644 --- a/app/src/main/java/com/philliphsu/clock2/stopwatch/StopwatchFragment.java +++ b/app/src/main/java/com/philliphsu/clock2/stopwatch/StopwatchFragment.java @@ -63,8 +63,6 @@ public class StopwatchFragment extends RecyclerViewFragment< mPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); mStartTime = mPrefs.getLong(KEY_START_TIME, 0); mPauseTime = mPrefs.getLong(KEY_PAUSE_TIME, 0); - // TODO: Any better solutions? - mActivityFab = new WeakReference<>((FloatingActionButton) getActivity().findViewById(R.id.fab)); Log.d(TAG, "mStartTime = " + mStartTime + ", mPauseTime = " + mPauseTime); } @@ -74,6 +72,11 @@ public class StopwatchFragment extends RecyclerViewFragment< public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { // TODO: Apply size span on chronom View view = super.onCreateView(inflater, container, savedInstanceState); + // TODO: Any better alternatives? + // TOneverDO: Move to onCreate(). It is not called again on device rotation, so we will + // have a null reference. + mActivityFab = new WeakReference<>((FloatingActionButton) getActivity().findViewById(R.id.fab)); + if (mStartTime > 0) { long base = mStartTime; if (mPauseTime > 0) {