Android Custom Sliding Menu

Android Custom Sliding Menu

package com.mobilhanem.slidingmenu;

 

import java.util.ArrayList;

import java.util.List;

 

import android.os.Bundle;

import android.app.Activity;

import android.app.Fragment;

import android.app.FragmentManager;

import android.content.res.Configuration;

import android.support.v4.app.ActionBarDrawerToggle;

import android.support.v4.view.GravityCompat;

import android.support.v4.widget.DrawerLayout;

import android.view.MenuItem;

import android.view.View;

import android.widget.AdapterView;

import android.widget.Button;

import android.widget.ListView;

import android.widget.RelativeLayout;

import android.widget.Toast;

 

/**

* Created by Alper on 1.2.2015.

*/

 

 

 

public class MainActivity extends Activity {

 

private DrawerLayout mDrawerLayout;

    private RelativeLayout mDrawerView;

private ListView mDrawerList;

private ActionBarDrawerToggle mDrawerToggle;

 

private CharSequence mDrawerTitle;

private CharSequence mTitle;

CustomDrawerAdapter adapter;

    private Button button_signIn;

 

 

List dataList;

 

 

 

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(com.mobilhanem.slidingmenu.R.layout.activity_main);

 

// Initializing

dataList = new ArrayList();

mTitle = mDrawerTitle = getTitle();

mDrawerLayout = (DrawerLayout) findViewById(com.mobilhanem.slidingmenu.R.id.drawer_layout);

mDrawerList = (ListView) findViewById(com.mobilhanem.slidingmenu.R.id.left_drawer);

 

mDrawerLayout.setDrawerShadow(com.mobilhanem.slidingmenu.R.drawable.drawer_shadow,

GravityCompat.START);

 

        mDrawerView = (RelativeLayout)findViewById(com.mobilhanem.slidingmenu.R.id.relativeLayout_slider_menu);

 

 

        button_signIn = (Button)findViewById(com.mobilhanem.slidingmenu.R.id.button_signIn);

        button_signIn.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

 

                Toast.makeText(getApplicationContext(),“Login Buton Tıklandı!!”,Toast.LENGTH_LONG).show();

            }

        });

 

// Add Drawer Item to dataList

dataList.add(new DrawerItem(“ANASAYFA”));

dataList.add(new DrawerItem(“ANDROID”));

dataList.add(new DrawerItem(“IOS”));

dataList.add(new DrawerItem(“WINDOWS PHONE”));

dataList.add(new DrawerItem(“SYMBIAN”));

dataList.add(new DrawerItem(“BLACKBERRY”));

        dataList.add(new DrawerItem(“WINDOWS MOBILE”));

        dataList.add(new DrawerItem(“BADA”));

        dataList.add(new DrawerItem(“MAEMO”));

        dataList.add(new DrawerItem(“TIZEN”));

dataList.add(new DrawerItem(“AYARLAR”));

        dataList.add(new DrawerItem(“YARDIM”));

 

 

 

 

adapter = new CustomDrawerAdapter(this, com.mobilhanem.slidingmenu.R.layout.custom_drawer_item,

dataList);

 

 

mDrawerList.setAdapter(adapter);

 

 

 

 

mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

 

getActionBar().setDisplayHomeAsUpEnabled(true);

getActionBar().setHomeButtonEnabled(true);

 

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,

com.mobilhanem.slidingmenu.R.drawable.ic_drawer, com.mobilhanem.slidingmenu.R.string.drawer_open,

com.mobilhanem.slidingmenu.R.string.drawer_close) {

public void onDrawerClosed(View view) {

getActionBar().setTitle(mDrawerTitle);

invalidateOptionsMenu(); // creates call to

// onPrepareOptionsMenu()

}

 

public void onDrawerOpened(View drawerView) {

getActionBar().setTitle(mDrawerTitle);

invalidateOptionsMenu(); // creates call to

// onPrepareOptionsMenu()

}

};

 

mDrawerLayout.setDrawerListener(mDrawerToggle);

 

if (savedInstanceState == null) {

SelectItem(0);

 

 

 

}

 

}

 

 

 

 

public void SelectItem(int possition) {

 

 

Fragment fragment = null;

Bundle args = new Bundle();

switch (possition) {

case 0:

 

            fragment = new FragmentHome();

            args.putString(FragmentHome.ITEM_NAME, dataList.get(possition).getItemName());

 

            break;

 

case 1:

 

            fragment = new FragmentAndroid();

args.putString(FragmentAndroid.ITEM_NAME, dataList.get(possition).getItemName());

 

break;

case 2:

fragment = new FragmentIOS();

args.putString(FragmentIOS.ITEM_NAME, dataList.get(possition).getItemName());

 

break;

case 3:

fragment = new FragmentWindowsPhone();

args.putString(FragmentWindowsPhone.ITEM_NAME, dataList.get(possition).getItemName());

 

break;

case 4:

fragment = new FragmentAndroid();

//args.putString(FragmentTwo.ITEM_NAME, dataList.get(possition).getItemName());

 

break;

case 5:

fragment = new FragmentIOS();

//args.putString(FragmentThree.ITEM_NAME, dataList.get(possition).getItemName());

 

break;

case 6:

fragment = new FragmentHome();

//args.putString(FragmentOne.ITEM_NAME, dataList.get(possition).getItemName());

 

break;

case 7:

fragment = new FragmentAndroid();

//args.putString(FragmentTwo.ITEM_NAME, dataList.get(possition).getItemName());

 

break;

case 8:

fragment = new FragmentIOS();

//args.putString(FragmentThree.ITEM_NAME, dataList.get(possition).getItemName());

 

break;

case 9:

fragment = new FragmentHome();

//args.putString(FragmentOne.ITEM_NAME, dataList.get(possition).getItemName());

 

break;

case 10:

fragment = new FragmentAndroid();

//args.putString(FragmentTwo.ITEM_NAME, dataList.get(possition).getItemName());

 

break;

case 11:

fragment = new FragmentIOS();

//args.putString(FragmentThree.ITEM_NAME, dataList.get(possition).getItemName());

 

break;

case 12:

fragment = new FragmentHome();

//args.putString(FragmentOne.ITEM_NAME, dataList.get(possition).getItemName());

 

break;

default:

break;

}

fragment.setArguments(args);

FragmentManager frgManager = getFragmentManager();

frgManager.beginTransaction().replace(com.mobilhanem.slidingmenu.R.id.content_frame, fragment)

.commit();

 

mDrawerList.setItemChecked(possition, true);

setTitle(dataList.get(possition).getItemName());

//mDrawerLayout.closeDrawer(mDrawerList);

        mDrawerLayout.closeDrawer(mDrawerView);

 

}

 

 

 

 

 

    @Override

public void setTitle(CharSequence title) {

mTitle = title;

getActionBar().setTitle(mTitle);

}

 

@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);

// Pass any configuration change to the drawer toggles

mDrawerToggle.onConfigurationChanged(newConfig);

}

 

@Override

public boolean onOptionsItemSelected(MenuItem item) {

// The action bar home/up action should open or close the drawer.

// ActionBarDrawerToggle will take care of this.

if (mDrawerToggle.onOptionsItemSelected(item)) {

return true;

}

 

return false;

}

 

private class DrawerItemClickListener implements

ListView.OnItemClickListener {

@Override

public void onItemClick(AdapterView<?> parent, View view, int position,

long id) {

 

SelectItem(position);

 

}

}

 

}

Yorum Yap
0 Yorum yapan