在布局文件中:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/lv"
android:background="#ff00ff"
android:layout_gravity="start"
android:layout_width="wrap_content"
android:layout_height="match_parent"></ListView>
</android.support.v4.widget.DrawerLayout>
在Activity中进行操作
/**
* 得到它的数据
*/
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer);
ListView lv = (ListView) findViewById(R.id.lv);
/**
* 设置适配器
*/
MyAdapter adapter = new MyAdapter(this);
lv.setAdapter(adapter);
/*
* listView点击事件
*/
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
vp.setCurrentItem(position);
//关闭
drawer.closeDrawer(lv);
}
});
自定义一个Adapter
public class MyAdapter extends BaseAdapter {
//设置数组的数据
String[] arr ={"首页","我的","个人"};
Context context;
public MyAdapter(Context context) {
this.context = context;
}
//得到数组的长度
@Override
public int getCount() {
return arr.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
/**
* 优化并展示侧拉需要展示的数据信息
*/
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
//得到TextView控件
TextView tv = new TextView(context);
tv.setText(arr[i]);
return tv;
}
}