Android抽屉菜单DrawerLayout的实现案例
(1)项目布局文件
activity_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<!-- The navigation view -->
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#ffffcc"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" >
</ListView>
</android.support.v4.widget.DrawerLayout>
fragment_content.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp" />
</LinearLayout>
(2)主要类文件
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<String> menuLists;
private ArrayAdapter<String> 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<String>();
for (int i = 0; i < 5; i++) {
menuLists.add("item" + i);
}
// 初始化适配器
adapter = new ArrayAdapter<String>(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;
}
/**
* 设置actionBar上边图标的点击事件
*/
@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);
}
/**
* 根据官方文档提示的信息
*
* 将mDrawerToggle.syncState();放入到onPostCreate中
*/
@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);
}
/**
* 监听事件的实现
*
*
* 当点击菜单栏中的item的时候切换相应的fragment界面
*/
@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);
}
}
ContentFragment.java
package com.xuliugen.drawerlayout;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* 用于填充界面的fragment
*
* @author xuliugen
*
*/
public class ContentFragment extends Fragment {
private TextView textView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_content, container,false);
textView = (TextView) view.findViewById(R.id.textView);
// 获得传入的参数
String text = getArguments().getString("text");
textView.setText(text);
return view;
}
}
(3)项目演示效果
(4)项目源代码和Google参考文档下载:http://yunpan.cn/cZZ7RVRY96yWe (提取码:2981)
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。