biz.mosil.demo.navigationdrawer.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for biz.mosil.demo.navigationdrawer.MainActivity.java

Source

/*
 * Copyright (C) 2013 Mosil(http://mosil.biz)
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package biz.mosil.demo.navigationdrawer;

import biz.mosil.demo.appcompat.R;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;

public class MainActivity extends ActionBarActivity {
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;

    private LinearLayout mLlvDrawerContent;
    private ListView mLsvDrawerMenu;
    // ??
    private int mCurrentMenuItemPosition = -1;
    // ?
    public static final String[] MENU_ITEMS = new String[] { "Item1", "Item2", "Item3", "Item4", "Item5" };

    private LinearLayout mLlvDrawerRight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drw_layout);
        //  Drawer ?
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, //  Drawer Toggle ???
                R.drawable.ic_drawer, // Drawer  Icon
                R.string.open_left_drawer, // Drawer ??
                R.string.close_left_drawer // Drawer ??
        ) {
            // ??
            @Override
            public void onDrawerOpened(View drawerView) {
                //  Title 
                if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
                    getSupportActionBar().setTitle(R.string.open_right_drawer);
                } else {
                    getSupportActionBar().setTitle(R.string.open_left_drawer);
                }

                if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
                    // ???
                    mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, Gravity.RIGHT);
                } else {
                    // ???
                    mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, Gravity.LEFT);
                }
            }

            // ??
            @Override
            public void onDrawerClosed(View drawerView) {
                if (mCurrentMenuItemPosition > -1) {
                    getSupportActionBar().setTitle(MENU_ITEMS[mCurrentMenuItemPosition]);
                } else {
                    //  Title  APP ??
                    getSupportActionBar().setTitle(R.string.app_name);
                }

                if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
                    // 
                    mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, Gravity.LEFT);
                } else if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
                    // ?
                    mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, Gravity.RIGHT);
                } else {
                    // ?
                    mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, Gravity.LEFT);
                    mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, Gravity.RIGHT);
                }
            }
        };

        mDrawerLayout.setDrawerListener(mDrawerToggle);
        //  Up Button (? Logo )
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        //  Up Button 
        getSupportActionBar().setHomeButtonEnabled(true);

        setDrawerMenu();
    }

    /**
     * ? - Drawer 
     * */
    private void setDrawerMenu() {
        // ? ListView ? Drawer LinearLayou
        mLsvDrawerMenu = (ListView) findViewById(R.id.lsv_drawer_menu);
        mLlvDrawerContent = (LinearLayout) findViewById(R.id.llv_left_drawer);

        // ????
        mLsvDrawerMenu.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectMenuItem(position);
            }
        });
        //  Adapter ArrayAdapter<String>
        mLsvDrawerMenu.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_menu_item, // ?? 
                MENU_ITEMS // ?
        ));
    }

    /**
     * ?????
     * */
    private void selectMenuItem(int position) {
        mCurrentMenuItemPosition = position;

        // ???
        mLsvDrawerMenu.setItemChecked(position, true);

        //  Drawer
        mDrawerLayout.closeDrawer(mLlvDrawerContent);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            //  DrawerLayout  Drawer 
            if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
                //  DrawerLayout ? Drawer
                mDrawerLayout.closeDrawer(Gravity.RIGHT);
            }
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}