tries to prevent a crash in the custom ViewPager

This commit is contained in:
y20k 2017-01-16 15:56:58 +01:00
parent 6ddcf3ae67
commit 93176e2a7a
3 changed files with 24 additions and 4 deletions

View file

@ -23,6 +23,7 @@ android {
dependencies { dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs') compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12' testCompile 'junit:junit:4.12'
compile 'com.android.support:support-v4:25.1.0'
compile 'com.android.support:appcompat-v7:25.1.0' compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:design:25.1.0' compile 'com.android.support:design:25.1.0'
compile 'com.android.support:cardview-v7:25.1.0' compile 'com.android.support:cardview-v7:25.1.0'

View file

@ -175,6 +175,17 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
startActivity(aboutIntent); startActivity(aboutIntent);
return true; return true;
// CASE MY LOCATION
case R.id.action_bar_my_location:
LogHelper.v(LOG_TAG, "MainActivity: CASE MY LOCATION."); // todo remove
if (mSelectedTab != FRAGMENT_ID_MAP) {
LogHelper.v(LOG_TAG, "MainActivity: ."); // todo remove
// show map fragment
mSelectedTab = FRAGMENT_ID_MAP;
mViewPager.setCurrentItem(mSelectedTab);
}
return false;
// CASE DEFAULT // CASE DEFAULT
default: default:
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
@ -215,6 +226,9 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
super.onDestroy(); super.onDestroy();
LogHelper.v(LOG_TAG, "onDestroy called."); LogHelper.v(LOG_TAG, "onDestroy called.");
// reset selected tab
mSelectedTab = FRAGMENT_ID_MAP;
// disable broadcast receiver // disable broadcast receiver
LocalBroadcastManager.getInstance(this).unregisterReceiver(mTrackingStoppedReceiver); LocalBroadcastManager.getInstance(this).unregisterReceiver(mTrackingStoppedReceiver);
} }
@ -264,7 +278,6 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
} }
@Override @Override
public void onRestoreInstanceState(Bundle savedInstanceState) { public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState); super.onRestoreInstanceState(savedInstanceState);
@ -321,11 +334,13 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
case FRAGMENT_ID_MAP: case FRAGMENT_ID_MAP:
// show the Floating Action Button // show the Floating Action Button
mFloatingActionButton.show(); mFloatingActionButton.show();
mSelectedTab = FRAGMENT_ID_MAP;
break; break;
case FRAGMENT_ID_TRACK: case FRAGMENT_ID_TRACK:
// hide the Floating Action Button - and its sub menu // hide the Floating Action Button - and its sub menu
mFloatingActionButton.hide(); mFloatingActionButton.hide();
showFloatingActionButtonMenu(false); showFloatingActionButtonMenu(false);
mSelectedTab = FRAGMENT_ID_TRACK;
break; break;
default: default:
// show the Floating Action Button // show the Floating Action Button

View file

@ -73,10 +73,14 @@ public class NonSwipeableViewPager extends ViewPager {
try { try {
Class<?> viewpager = ViewPager.class; Class<?> viewpager = ViewPager.class;
Field scroller = viewpager.getDeclaredField("mScroller"); Field scroller = viewpager.getDeclaredField("mScroller");
scroller.setAccessible(true); if (scroller != null) {
scroller.set(this, new MyScroller(getContext())); scroller.setAccessible(true);
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."); LogHelper.e(LOG_TAG, "Problem accessing or modifying the mScroller field. Exception: " + e);
e.printStackTrace(); e.printStackTrace();
} }
} }