Change ArrayList to SparseArray for ViewPager adapter's collection of Fragments
This commit is contained in:
parent
04158bb6a8
commit
b1f3c9c8c7
@ -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<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) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user