Change ArrayList to SparseArray for ViewPager adapter's collection of Fragments

This commit is contained in:
Phillip Hsu 2016-08-14 01:23:15 -07:00
parent 04158bb6a8
commit b1f3c9c8c7
2 changed files with 15 additions and 13 deletions

View File

@ -10,7 +10,7 @@ import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager;
import android.util.Log; import android.util.SparseArray;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
@ -22,8 +22,6 @@ import com.philliphsu.clock2.settings.SettingsActivity;
import com.philliphsu.clock2.stopwatch.StopwatchFragment; import com.philliphsu.clock2.stopwatch.StopwatchFragment;
import com.philliphsu.clock2.timers.TimersFragment; import com.philliphsu.clock2.timers.TimersFragment;
import java.util.ArrayList;
import butterknife.Bind; import butterknife.Bind;
public class MainActivity extends BaseActivity { public class MainActivity extends BaseActivity {
@ -52,9 +50,11 @@ public class MainActivity extends BaseActivity {
@Bind(R.id.fab) @Bind(R.id.fab)
FloatingActionButton mFab; FloatingActionButton mFab;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(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 // Create the adapter that will return a fragment for each of the three
// primary sections of the activity. // primary sections of the activity.
@ -276,7 +276,10 @@ public class MainActivity extends BaseActivity {
* one of the sections/tabs/pages. * one of the sections/tabs/pages.
*/ */
private static class SectionsPagerAdapter extends FragmentPagerAdapter { private static class SectionsPagerAdapter extends FragmentPagerAdapter {
private final ArrayList<Fragment> 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<Fragment> mFragments = new SparseArray<>(getCount());
public SectionsPagerAdapter(FragmentManager fm) { public SectionsPagerAdapter(FragmentManager fm) {
super(fm); super(fm);
@ -300,15 +303,13 @@ public class MainActivity extends BaseActivity {
@Override @Override
public Object instantiateItem(ViewGroup container, int position) { public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position); Fragment fragment = (Fragment) super.instantiateItem(container, position);
mFragments.add(position, fragment); mFragments.put(position, fragment);
Log.d(TAG, "Instantiated fragment " + fragment + " for position " + position + ". New size = " + mFragments.size());
return fragment; return fragment;
} }
@Override @Override
public void destroyItem(ViewGroup container, int position, Object object) { public void destroyItem(ViewGroup container, int position, Object object) {
Fragment f = mFragments.remove(position); mFragments.remove(position);
Log.d(TAG, "Destroyed fragment " + f + " for position " + position + ". New size = " + mFragments.size());
super.destroyItem(container, position, object); super.destroyItem(container, position, object);
} }
@ -332,9 +333,7 @@ public class MainActivity extends BaseActivity {
} }
public Fragment getFragment(int position) { public Fragment getFragment(int position) {
Fragment f = mFragments.get(position); return mFragments.get(position);
Log.d(TAG, "Returning fragment " + f + " for position " + position);
return f;
} }
} }
} }

View File

@ -63,8 +63,6 @@ public class StopwatchFragment extends RecyclerViewFragment<
mPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); mPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
mStartTime = mPrefs.getLong(KEY_START_TIME, 0); mStartTime = mPrefs.getLong(KEY_START_TIME, 0);
mPauseTime = mPrefs.getLong(KEY_PAUSE_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 Log.d(TAG, "mStartTime = " + mStartTime
+ ", mPauseTime = " + mPauseTime); + ", mPauseTime = " + mPauseTime);
} }
@ -74,6 +72,11 @@ public class StopwatchFragment extends RecyclerViewFragment<
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// TODO: Apply size span on chronom // TODO: Apply size span on chronom
View view = super.onCreateView(inflater, container, savedInstanceState); 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) { if (mStartTime > 0) {
long base = mStartTime; long base = mStartTime;
if (mPauseTime > 0) { if (mPauseTime > 0) {