From d835a9b8f38acf1970acc18ce2030f7d5f7377dd Mon Sep 17 00:00:00 2001 From: Fup Duck Date: Tue, 28 Nov 2017 01:51:19 +0100 Subject: base for sidebar --- .../java/se/leap/bitmaskclient/MainActivity.java | 115 +++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 app/src/main/java/se/leap/bitmaskclient/MainActivity.java (limited to 'app/src/main/java/se/leap/bitmaskclient/MainActivity.java') diff --git a/app/src/main/java/se/leap/bitmaskclient/MainActivity.java b/app/src/main/java/se/leap/bitmaskclient/MainActivity.java new file mode 100644 index 00000000..d28d0784 --- /dev/null +++ b/app/src/main/java/se/leap/bitmaskclient/MainActivity.java @@ -0,0 +1,115 @@ +package se.leap.bitmaskclient; + + +import android.content.Context; +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; +import android.support.v7.app.ActionBar; +import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.Toolbar; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.support.v4.widget.DrawerLayout; + +public class MainActivity extends AppCompatActivity + implements NavigationDrawerFragment.NavigationDrawerCallbacks { + + /** + * 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); + + setSupportActionBar((Toolbar) findViewById(R.id.toolbar)); + + mNavigationDrawerFragment = (NavigationDrawerFragment) + getSupportFragmentManager().findFragmentById(R.id.navigation_drawer); + mTitle = getTitle(); + + // Set up the drawer. + mNavigationDrawerFragment.setUp( + R.id.navigation_drawer, + (DrawerLayout) findViewById(R.id.drawer_layout)); + } + + @Override + public void onNavigationDrawerItemSelected(int position) { + // update the main content by replacing fragments + FragmentManager fragmentManager = getSupportFragmentManager(); + fragmentManager.beginTransaction() + .replace(R.id.container, PlaceholderFragment.newInstance(position + 1)) + .commit(); + } + + public void onSectionAttached(int number) { + switch (number) { + case 1: + mTitle = getString(R.string.title_section1); + break; + case 2: + mTitle = getString(R.string.title_section2); + break; + case 3: + mTitle = getString(R.string.title_section3); + break; + } + } + + public void restoreActionBar() { + ActionBar actionBar = getSupportActionBar(); + actionBar.setDisplayShowTitleEnabled(true); + actionBar.setTitle(mTitle); + } + + /** + * A placeholder fragment containing a simple view. + */ + public static class PlaceholderFragment extends Fragment { + /** + * The fragment argument representing the section number for this + * fragment. + */ + private static final String ARG_SECTION_NUMBER = "section_number"; + + public PlaceholderFragment() { + } + + /** + * Returns a new instance of this fragment for the given section + * number. + */ + public static PlaceholderFragment newInstance(int sectionNumber) { + PlaceholderFragment fragment = new PlaceholderFragment(); + Bundle args = new Bundle(); + args.putInt(ARG_SECTION_NUMBER, sectionNumber); + fragment.setArguments(args); + return fragment; + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View rootView = inflater.inflate(R.layout.fragment_main, container, false); + return rootView; + } + + @Override + public void onAttach(Context context) { + super.onAttach(context); + ((MainActivity) context).onSectionAttached( + getArguments().getInt(ARG_SECTION_NUMBER)); + } + } + +} -- cgit v1.2.3