Back to project page GPSDataCollector.
The source code is released under:
GNU General Public License
If you think the Android project GPSDataCollector listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.ymelo.gpsdatacollector.app; //ww w.jav a 2 s .c om import android.app.*; import android.os.Bundle; import android.support.v4.app.*; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.view.ViewPager; import android.view.Menu; import android.view.MenuItem; import android.support.v4.widget.DrawerLayout; import java.util.Locale; import static java.util.Locale.*; public class MainActivity extends FragmentActivity implements NavigationDrawerFragment.NavigationDrawerCallbacks { private ViewPager mViewPager; /** * Fragment managing the behaviors, interactions and presentation of the navigation drawer. */ private NavigationDrawerFragment mNavigationDrawerFragment; /** * Used to store the last screen title. For use in {@link #restoreActionBar()}. */ private CharSequence mTitle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mViewPager = (ViewPager) findViewById(R.id.view_pager); mViewPager.setAdapter(new SectionsPagerAdapter(getSupportFragmentManager())); mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.navigation_drawer); mTitle = getTitle(); // Set up the drawer. mNavigationDrawerFragment.setUp( R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout)); } /** * A {@link FragmentPagerAdapter} that returns a fragment corresponding to * one of the sections/tabs/pages. */ public class SectionsPagerAdapter extends FragmentPagerAdapter { public SectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { FragmentManager fragmentManager = getSupportFragmentManager(); Fragment fr = null; String tag = null; switch (position) { case 1: tag = RecordFragment.TAG; fr = fragmentManager.findFragmentByTag(tag); if(fr == null) { fr = new RecordFragment(); fr.setRetainInstance(true); // fragmentManager.beginTransaction() // .add(R.id.fragment_container, fr, tag) // .commit(); } break; case 0: tag = DisplayFragment.TAG; fr = fragmentManager.findFragmentByTag(tag); if(fr == null) { fr = new DisplayFragment(); fr.setRetainInstance(true); // fragmentManager.beginTransaction() // .add(R.id.fragment_container, fr, tag) // .commit(); } break; // default: // tag = MapFragment.TAG; // fr = getFragmentManager().findFragmentByTag(tag); // if(fr == null) { // fr = MapFragment.newInstance("" + position + 1); // } // // break; } return fr; } @Override public int getCount() { // Show 3 total pages. return 2; } @Override public CharSequence getPageTitle(int position) { Locale l = getDefault(); switch (position) { case 0: return getString(R.string.title_section1).toUpperCase(l); case 1: return getString(R.string.title_section2).toUpperCase(l); case 2: return getString(R.string.title_section3).toUpperCase(l); } return null; } } @Override public void onNavigationDrawerItemSelected(int position) { // update the main content by replacing fragments if(mViewPager != null) { mViewPager.setCurrentItem(position); } } public void onSectionAttached(String title) { mTitle = title; } public void restoreActionBar() { ActionBar actionBar = getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); actionBar.setDisplayShowTitleEnabled(true); actionBar.setTitle(mTitle); } @Override public boolean onCreateOptionsMenu(Menu menu) { if (!mNavigationDrawerFragment.isDrawerOpen()) { // Only show items in the action bar relevant to this screen // if the drawer is not showing. Otherwise, let the drawer // decide what to show in the action bar. getMenuInflater().inflate(R.menu.main, menu); restoreActionBar(); return true; } return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } public void onBackPressed() { if (!getSupportFragmentManager().popBackStackImmediate()) { finish(); } } }