天天看點

android 抽屜 例子,Android抽屜菜單DrawerLayout的實作案例

package com.xuliugen.drawerlayout;

import java.util.ArrayList;

import android.app.Activity;

import android.app.Fragment;

import android.app.FragmentManager;

import android.content.Intent;

import android.content.res.Configuration;

import android.net.Uri;

import android.os.Bundle;

import android.support.v4.app.ActionBarDrawerToggle;

import android.support.v4.widget.DrawerLayout;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.ArrayAdapter;

import android.widget.ListView;

public class MainActivity extends Activity implements OnItemClickListener {

private DrawerLayout mDrawerLayout; // 設定的是左側的抽屜菜單

private ListView mDrawerList;

private ArrayList menuLists;

private ArrayAdapter adapter;

private ActionBarDrawerToggle mDrawerToggle;// actionBar打開關閉的

private String mTitle;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mTitle = (String) getTitle();

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

mDrawerList = (ListView) findViewById(R.id.left_drawer);

menuLists = new ArrayList();

for (int i = 0; i < 5; i++) {

menuLists.add("item" + i);

}

// 初始化擴充卡

adapter = new ArrayAdapter(this,

android.R.layout.simple_list_item_1, menuLists);

// 為左側的抽屜設定了資料

mDrawerList.setAdapter(adapter);

// 左側滑動菜單的監聽事件

mDrawerList.setOnItemClickListener(this);

// 設定抽屜被打開關閉的對象

mDrawerToggle = new ActionBarDrawerToggle(this, //

mDrawerLayout,//

R.drawable.ic_drawer,//

R.string.drawer_open,//

R.string.drawer_close) {

// 被打開的時候

@Override

public void onDrawerOpened(View drawerView) {

super.onDrawerOpened(drawerView);

getActionBar().setTitle("請選擇"); // 設定actionBar的文字

invalidateOptionsMenu(); // Call onPrepareOptionsMenu()

}

// 被關閉的時候

@Override

public void onDrawerClosed(View drawerView) {

super.onDrawerClosed(drawerView);

getActionBar().setTitle(mTitle);

invalidateOptionsMenu();// 又一次繪制actionBar上邊的菜單項

}

};

// 設定滑動菜單的 打開關閉事件

mDrawerLayout.setDrawerListener(mDrawerToggle);

// 開啟ActionBar上APP ICON的功能:點選打開和點選關閉7

getActionBar().setDisplayHomeAsUpEnabled(true);

getActionBar().setHomeButtonEnabled(true);

}

@Override

public boolean onPrepareOptionsMenu(Menu menu) {

boolean isDrawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);

menu.findItem(R.id.action_websearch).setVisible(!isDrawerOpen);

return super.onPrepareOptionsMenu(menu);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

// 将ActionBar上的圖示與Drawer結合起來

if (mDrawerToggle.onOptionsItemSelected(item)) {

return true;

}

switch (item.getItemId()) {

case R.id.action_websearch:

Intent intent = new Intent();

intent.setAction("android.intent.action.VIEW");

Uri uri = Uri.parse("http://blog.csdn.net/xlgen157387");

intent.setData(uri);

startActivity(intent);

break;

}

return super.onOptionsItemSelected(item);

}

@Override

protected void onPostCreate(Bundle savedInstanceState) {

super.onPostCreate(savedInstanceState);

// 須要将ActionDrawerToggle與DrawerLayout的狀态同步

// 将ActionBarDrawerToggle中的drawer圖示,設定為ActionBar中的Home-Button的Icon

mDrawerToggle.syncState();

}

@Override

public void onConfigurationChanged(Configuration newConfig) {

super.onConfigurationChanged(newConfig);

mDrawerToggle.onConfigurationChanged(newConfig);

}

@Override

public void onItemClick(AdapterView> arg0, View arg1, int position,

long arg3) {

// 動态插入一個Fragment到FrameLayout其中

Fragment contentFragment = new ContentFragment();

Bundle bundle = new Bundle();

bundle.putString("text", menuLists.get(position));

contentFragment.setArguments(bundle);

// fragment建立好了之後須要交給fragmentManager來替換到對應的視圖中

FragmentManager fm = getFragmentManager();

fm.beginTransaction().replace(R.id.content_frame, contentFragment)

.commit();

mDrawerLayout.closeDrawer(mDrawerList);

}

}