changed the way the fragments (= tab contents) are referenced from the frame ui (MainActivity)

This commit is contained in:
y20k 2017-01-17 10:40:47 +01:00
parent ed0f0db464
commit ce452905fe
2 changed files with 63 additions and 31 deletions

View file

@ -29,6 +29,7 @@ import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar; import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout; import android.support.design.widget.TabLayout;
@ -39,6 +40,7 @@ import android.support.v4.content.ContextCompat;
import android.support.v4.content.LocalBroadcastManager; import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.util.SparseArray;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
@ -52,6 +54,7 @@ import org.y20k.trackbook.helpers.NotificationHelper;
import org.y20k.trackbook.helpers.TrackbookKeys; import org.y20k.trackbook.helpers.TrackbookKeys;
import org.y20k.trackbook.layout.NonSwipeableViewPager; import org.y20k.trackbook.layout.NonSwipeableViewPager;
import java.lang.ref.WeakReference;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -68,6 +71,7 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
/* Main class variables */ /* Main class variables */
private SectionsPagerAdapter mSectionsPagerAdapter;
private NonSwipeableViewPager mViewPager; private NonSwipeableViewPager mViewPager;
private boolean mTrackerServiceRunning; private boolean mTrackerServiceRunning;
// private boolean mCurrentTrackVisible; // private boolean mCurrentTrackVisible;
@ -77,8 +81,8 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
private FloatingActionButton mFloatingActionButton; private FloatingActionButton mFloatingActionButton;
private LinearLayout mFloatingActionButtonSubMenu1; private LinearLayout mFloatingActionButtonSubMenu1;
private LinearLayout mFloatingActionButtonSubMenu2; private LinearLayout mFloatingActionButtonSubMenu2;
private MainActivityMapFragment mMainActivityMapFragment; // private MainActivityMapFragment mMainActivityMapFragment;
private MainActivityTrackFragment mMainActivityTrackFragment; // private MainActivityTrackFragment mMainActivityTrackFragment;
private BroadcastReceiver mTrackingStoppedReceiver; private BroadcastReceiver mTrackingStoppedReceiver;
private int mFloatingActionButtonState; private int mFloatingActionButtonState;
private int mSelectedTab; private int mSelectedTab;
@ -318,11 +322,11 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
// create adapter that returns fragments for the maim map and the last track display // create adapter that returns fragments for the maim map and the last track display
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter. // Set up the ViewPager with the sections adapter.
mViewPager = (NonSwipeableViewPager) findViewById(R.id.container); mViewPager = (NonSwipeableViewPager) findViewById(R.id.container);
mViewPager.setAdapter(sectionsPagerAdapter); mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setCurrentItem(mSelectedTab); mViewPager.setCurrentItem(mSelectedTab);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
@ -435,7 +439,9 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
setFloatingActionButtonState(); setFloatingActionButtonState();
// get last location from MainActivity Fragment // get last location from MainActivity Fragment
Location lastLocation = mMainActivityMapFragment.getCurrentBestLocation(); // Location lastLocation = mMainActivityMapFragment.getCurrentBestLocation();
MainActivityMapFragment mainActivityMapFragment = (MainActivityMapFragment) mSectionsPagerAdapter.getFragment(FRAGMENT_ID_MAP);
Location lastLocation = mainActivityMapFragment.getCurrentBestLocation();
if (lastLocation != null) { if (lastLocation != null) {
// start tracker service // start tracker service
@ -479,7 +485,8 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
} }
// update tracking state in MainActivityMapFragment // update tracking state in MainActivityMapFragment
mMainActivityMapFragment.setTrackingState(mTrackerServiceRunning); MainActivityMapFragment mainActivityMapFragment = (MainActivityMapFragment) mSectionsPagerAdapter.getFragment(FRAGMENT_ID_MAP);
mainActivityMapFragment.setTrackingState(mTrackerServiceRunning);
} }
@ -488,7 +495,8 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
LogHelper.v(LOG_TAG, "User chose SAVE and CLEAR"); LogHelper.v(LOG_TAG, "User chose SAVE and CLEAR");
// clear map and save track // clear map and save track
mMainActivityMapFragment.clearMap(true); MainActivityMapFragment mainActivityMapFragment = (MainActivityMapFragment) mSectionsPagerAdapter.getFragment(FRAGMENT_ID_MAP);
mainActivityMapFragment.clearMap(true);
// mCurrentTrackVisible = false; // mCurrentTrackVisible = false;
// display and update track tab // display and update track tab
@ -513,7 +521,8 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
LogHelper.v(LOG_TAG, "User chose CLEAR"); LogHelper.v(LOG_TAG, "User chose CLEAR");
// clear map, do not save track // clear map, do not save track
mMainActivityMapFragment.clearMap(false); MainActivityMapFragment mainActivityMapFragment = (MainActivityMapFragment) mSectionsPagerAdapter.getFragment(FRAGMENT_ID_MAP);
mainActivityMapFragment.clearMap(false);
// mCurrentTrackVisible = false; // mCurrentTrackVisible = false;
// dismiss notification // dismiss notification
@ -621,7 +630,8 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
setFloatingActionButtonState(); setFloatingActionButtonState();
// pass tracking state to MainActivityMapFragment // pass tracking state to MainActivityMapFragment
mMainActivityMapFragment.setTrackingState(false); MainActivityMapFragment mainActivityMapFragment = (MainActivityMapFragment) mSectionsPagerAdapter.getFragment(FRAGMENT_ID_MAP);
mainActivityMapFragment.setTrackingState(false);
} }
}; };
} }
@ -630,9 +640,12 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
/** /**
* Inner class: SectionsPagerAdapter that returns a fragment corresponding to one of the tabs. * Inner class: SectionsPagerAdapter that returns a fragment corresponding to one of the tabs.
* see also: https://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html * see also: https://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html
* and: http://www.truiton.com/2015/12/android-activity-fragment-communication/
*/ */
public class SectionsPagerAdapter extends FragmentPagerAdapter { public class SectionsPagerAdapter extends FragmentPagerAdapter {
private final SparseArray<WeakReference<Fragment>> instantiatedFragments = new SparseArray<>();
public SectionsPagerAdapter(FragmentManager fm) { public SectionsPagerAdapter(FragmentManager fm) {
super(fm); super(fm);
} }
@ -667,19 +680,43 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
} }
@Override @Override
public Object instantiateItem(ViewGroup container, int position) { public Object instantiateItem(final ViewGroup container, final int position) {
Fragment createdFragment = (Fragment) super.instantiateItem(container, position); final Fragment fragment = (Fragment) super.instantiateItem(container, position);
// save references to created Fragments instantiatedFragments.put(position, new WeakReference<>(fragment));
switch (position) { return fragment;
case FRAGMENT_ID_MAP:
mMainActivityMapFragment = (MainActivityMapFragment)createdFragment;
break;
case FRAGMENT_ID_TRACK:
mMainActivityTrackFragment = (MainActivityTrackFragment)createdFragment;
break;
} }
return createdFragment;
@Override
public void destroyItem(final ViewGroup container, final int position, final Object object) {
instantiatedFragments.remove(position);
super.destroyItem(container, position, object);
} }
// @Override
// public Object instantiateItem(ViewGroup container, int position) {
// Fragment createdFragment = (Fragment) super.instantiateItem(container, position);
// // save references to created Fragments
// switch (position) {
// case FRAGMENT_ID_MAP:
// mMainActivityMapFragment = (MainActivityMapFragment)createdFragment;
// break;
// case FRAGMENT_ID_TRACK:
// mMainActivityTrackFragment = (MainActivityTrackFragment)createdFragment;
// break;
// }
// return createdFragment;
// }
@Nullable
public Fragment getFragment(final int position) {
final WeakReference<Fragment> wr = instantiatedFragments.get(position);
if (wr != null) {
return wr.get();
} else {
return null;
}
}
} }
/** /**
* End of inner class * End of inner class

View file

@ -42,14 +42,14 @@ public class NonSwipeableViewPager extends ViewPager {
/* Constructor */ /* Constructor */
public NonSwipeableViewPager(Context context) { public NonSwipeableViewPager(Context context) {
super(context); super(context);
// setMyScroller(); setMyScroller();
} }
/* Constructor */ /* Constructor */
public NonSwipeableViewPager(Context context, AttributeSet attrs) { public NonSwipeableViewPager(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
// setMyScroller(); setMyScroller();
} }
@ -66,19 +66,14 @@ public class NonSwipeableViewPager extends ViewPager {
return false; return false;
} }
//down one is added for smooth scrolling
/* Attaches a custom scroller to a ViewPager */ /* Attaches a custom smooth scrolling scroller to a ViewPager */
private void setMyScroller() { private void setMyScroller() {
try { try {
Class<?> viewpager = ViewPager.class; Class<?> viewpager = ViewPager.class;
Field scroller = viewpager.getDeclaredField("mScroller"); Field scroller = viewpager.getDeclaredField("mScroller");
if (scroller != null) {
scroller.setAccessible(true); scroller.setAccessible(true);
scroller.set(this, new MyScroller(getContext())); scroller.set(this, new MyScroller(getContext()));
} else {
LogHelper.v(LOG_TAG, "Unable to get mScroller field."); // todo remove
}
} catch (Exception e) { } catch (Exception e) {
LogHelper.e(LOG_TAG, "Problem accessing or modifying the mScroller field. Exception: " + e); LogHelper.e(LOG_TAG, "Problem accessing or modifying the mScroller field. Exception: " + e);
e.printStackTrace(); e.printStackTrace();