Fixed bug where scrolling the ViewPager and quickly clicking the FAB called the previous Fragment's onFabClick()

This commit is contained in:
Phillip Hsu 2016-08-14 00:41:25 -07:00
parent 15afc01735
commit 04158bb6a8
2 changed files with 18 additions and 23 deletions

View File

@ -10,6 +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.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
@ -21,6 +22,8 @@ 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 {
@ -191,16 +194,11 @@ public class MainActivity extends BaseActivity {
// // because we want the result to be handled in the Fragment, not in this Activity. // // because we want the result to be handled in the Fragment, not in this Activity.
// // FragmentActivity does NOT deliver the result to the Fragment, i.e. your // // FragmentActivity does NOT deliver the result to the Fragment, i.e. your
// // Fragment's onActivityResult() will NOT be called. // // Fragment's onActivityResult() will NOT be called.
// mSectionsPagerAdapter.getCurrentFragment() // mSectionsPagerAdapter.getFragment()
// .startActivityForResult(intent, AlarmsFragment.REQUEST_CREATE_ALARM); // .startActivityForResult(intent, AlarmsFragment.REQUEST_CREATE_ALARM);
// TODO: If the user switches between pages and is quick enough to hit the Fragment f = mSectionsPagerAdapter.getFragment(mViewPager.getCurrentItem());
// fab before the target page comes fully into view, the onFabClick() if (f instanceof RecyclerViewFragment) {
// implementation of the previous page will be called. Perhaps do something
// with the current page from the ViewPager instead. E.g. monolithically handle
// each Fragment's fab click reaction in this activity.
Fragment f;
if ((f = mSectionsPagerAdapter.getCurrentFragment()) instanceof RecyclerViewFragment) {
((RecyclerViewFragment) f).onFabClick(); ((RecyclerViewFragment) f).onFabClick();
} }
} }
@ -278,8 +276,7 @@ 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());
private Fragment mCurrentFragment;
public SectionsPagerAdapter(FragmentManager fm) { public SectionsPagerAdapter(FragmentManager fm) {
super(fm); super(fm);
@ -301,18 +298,17 @@ public class MainActivity extends BaseActivity {
} }
@Override @Override
public void setPrimaryItem(ViewGroup container, int position, Object object) { public Object instantiateItem(ViewGroup container, int position) {
super.setPrimaryItem(container, position, object); Fragment fragment = (Fragment) super.instantiateItem(container, position);
if (mCurrentFragment != object) { mFragments.add(position, fragment);
mCurrentFragment = (Fragment) object; Log.d(TAG, "Instantiated fragment " + fragment + " for position " + position + ". New size = " + mFragments.size());
} return fragment;
} }
@Override @Override
public void destroyItem(ViewGroup container, int position, Object object) { public void destroyItem(ViewGroup container, int position, Object object) {
if (mCurrentFragment == object) { Fragment f = mFragments.remove(position);
mCurrentFragment = null; Log.d(TAG, "Destroyed fragment " + f + " for position " + position + ". New size = " + mFragments.size());
}
super.destroyItem(container, position, object); super.destroyItem(container, position, object);
} }
@ -335,8 +331,10 @@ public class MainActivity extends BaseActivity {
return null; return null;
} }
public Fragment getCurrentFragment() { public Fragment getFragment(int position) {
return mCurrentFragment; Fragment f = mFragments.get(position);
Log.d(TAG, "Returning fragment " + f + " for position " + position);
return f;
} }
} }
} }

View File

@ -161,9 +161,6 @@ public class StopwatchFragment extends RecyclerViewFragment<
} }
} else { } else {
if (mStartTime == 0) { if (mStartTime == 0) {
// TODO: I'm strongly considering inserting the very first lap alone.
// We'll need to tell the adapter to just hide the corresponding VH
// until a second lap is added.
// addNewLap() won't call through unless chronometer is running, which // addNewLap() won't call through unless chronometer is running, which
// we can't start until we compute mStartTime // we can't start until we compute mStartTime
mCurrentLap = new Lap(); mCurrentLap = new Lap();