Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.ref_apps.winnerplayer;
- import android.content.Context;
- import android.content.res.Configuration;
- import android.content.res.Resources;
- import android.content.res.TypedArray;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.support.v4.app.FragmentManager;
- import android.support.v4.widget.DrawerLayout;
- //import android.support.v7.app.ActionBarActivity;
- import android.support.v7.app.ActionBarDrawerToggle;
- import android.support.v7.app.AppCompatActivity;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- private ListView mDrawerList;
- private DrawerLayout mDrawerLayout;
- private ArrayAdapter<String> mAdapter;
- private ActionBarDrawerToggle mDrawerToggle;
- private String mActivityTitle;
- Context ctx;
- Resources res;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- ctx = getApplicationContext();
- res = ctx.getResources();
- mDrawerList = (ListView)findViewById(R.id.navList);
- mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
- mActivityTitle = getTitle().toString();
- addDrawerItems();
- setupDrawer();
- getSupportActionBar().setDisplayHomeAsUpEnabled(true);
- getSupportActionBar().setHomeButtonEnabled(true);
- }
- private void addDrawerItems() {
- String[] options = res.getStringArray(R.array.option_names);
- TypedArray icons = res.obtainTypedArray(R.array.option_icons);
- mAdapter = (new com.example.ref_apps.winnerplayer.ImageAndTextAdapter(ctx, R.layout.list_layout, options, icons));
- mDrawerList.setAdapter(mAdapter);
- mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- //Toast.makeText(MainActivity.this, "Time for an upgrade! " + position, Toast.LENGTH_SHORT).show();
- selectDrawerItem(position);
- }
- });
- }
- private void setupDrawer() {
- mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
- /** Called when a drawer has settled in a completely open state. */
- public void onDrawerOpened(View drawerView) {
- super.onDrawerOpened(drawerView);
- getSupportActionBar().setTitle("Make your choice");
- invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
- }
- /** Called when a drawer has settled in a completely closed state. */
- public void onDrawerClosed(View view) {
- super.onDrawerClosed(view);
- getSupportActionBar().setTitle(mActivityTitle);
- invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
- }
- };
- mDrawerToggle.setDrawerIndicatorEnabled(true);
- mDrawerLayout.setDrawerListener(mDrawerToggle);
- }
- public void selectDrawerItem(int option) {
- // Create a new fragment and specify the planet to show based on
- // position
- Fragment fragment = null;
- Class fragmentClass;
- //TODO when more fragments are invented - add cases!! 10/9/15
- switch(option) {
- case 0:
- fragmentClass = FragmentVideo.class;
- break;
- case 1:
- fragmentClass = FragmentAudio.class;
- break;
- default:
- fragmentClass = FragmentAudio.class;
- }
- try {
- fragment = (Fragment) fragmentClass.newInstance();
- } catch (Exception e) {
- e.printStackTrace();
- }
- // Insert the fragment by replacing any existing fragment
- FragmentManager fragmentManager = getSupportFragmentManager();
- fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
- // Highlight the selected item, update the title, and close the drawer
- mDrawerLayout.closeDrawers();
- }
- @Override
- protected void onPostCreate(Bundle savedInstanceState) {
- super.onPostCreate(savedInstanceState);
- // Sync the toggle state after onRestoreInstanceState has occurred.
- mDrawerToggle.syncState();
- }
- @Override
- public void onConfigurationChanged(Configuration newConfig) {
- super.onConfigurationChanged(newConfig);
- mDrawerToggle.onConfigurationChanged(newConfig);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @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;
- }
- // Activate the navigation drawer toggle
- if (mDrawerToggle.onOptionsItemSelected(item)) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement