文/程序员男神
前言
精力有限,分享无限。作为一个还在学习他人代码的码农,深知分享的好处。行走在别人博客路上的拾荒者,慢慢学会自己积累。最近有点忙啊,一直在赶项目的路上,突然想高唱一首歌放松一下,一人我编程累......
概述
先上需求图:DrawerLayout侧滑菜单的实现。
实现的功能细节:
Drawerlayout是Android v4 包里自带的,既然是自带的那么直接拿来用就可以了,当然前提是你得工程里有 v4 包,下面解释上面的布局文件,让你懂得Drawerlayout用法。
首先Drawerlayout支持左划和右划,那它是如何控制的呢?
慢慢告诉你,以上布局分为三部分,一般情况下,第一部分是主步局,第二部分是左划的布局,第三部分是右划的布局,其实这里的左向滑动和右向滑动是通过gravity控制,左划界面android:layout_gravity="left" 当然这里的left也可以用start代替,右划界面就理所当然的是android:layout_gravity="right" ,同样right也可以用end代替,其余的应该明白了吧!
注意:drawerlayout实现的侧滑,侧滑部分不响应点击事件,点击之后只是侧滑栏收进去,button捕获不到点击事件。
解决办法:http://bbs.csdn.net/topics/391918297
drawerlayout必须放在主界面布局后面。
实现步骤:
这篇文章是借着自定义TopBar内容的基础上实现的......
直接上代码:activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include layout="@layout/layout_topbars" />
<include
android:id="@+id/drawer_include"
layout="@layout/layout_drawer" />
</android.support.v4.widget.DrawerLayout>
layout_topbars.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.example.djj.drawerlayout.view.TopBar
android:id="@+id/topbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/colorPrimaryDark"
app:leftBackground="@drawable/right_button_selector"
app:rightBackground="@drawable/left_button_selector"
app:titleText="我的首页"
app:titleTextColor="#FFF"
app:titleTextSize="6sp"/>
<ListView
android:id="@+id/progress_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
layout_drawer.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:layout_gravity="start"
android:layout_marginRight="56dp"
android:background="#FFF"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="36dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="36dp"
android:src="@mipmap/zy_cd_ys" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:orientation="vertical">
<TextView
android:id="@+id/tv_menu_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="啦啦啦(5463891)" />
<TextView
android:id="@+id/tv_menu_area"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:text="xxx病区"
android:textColor="#FFF"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
<ListView
android:id="@+id/left_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"
android:scrollbars="none" />
</LinearLayout>
以上都是一些布局代码,一些资源文件后面有传送门。
接下来准备我们的适配器adapter的代码:
/**
* desc: 侧滑菜单的Adapter
* author: dj
* date: 2017/3/30 15:10
*/
public class ContentAdapter extends BaseAdapter {
private Context context;
private List<ContentModel> list;
public ContentAdapter(Context context, List<ContentModel> list) {
super();
this.context = context;
this.list = list;
}
@Override
public int getCount() {
if (list != null) {
return list.size();
}
return 0;
}
@Override
public Object getItem(int position) {
if (list != null) {
return list.get(position);
}
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHold hold;
if (convertView == null) {
hold = new ViewHold();
convertView = LayoutInflater.from(context).inflate(
R.layout.content_item, null);
convertView.setTag(hold);
} else {
hold = (ViewHold) convertView.getTag();
}
hold.imageView = (ImageView) convertView.findViewById(R.id.item_imageview);
hold.textView = (TextView) convertView.findViewById(R.id.item_textview);
hold.imageView.setImageResource(list.get(position).getImageView());
hold.textView.setText(list.get(position).getText());
return convertView;
}
static class ViewHold {
public ImageView imageView;
public TextView textView;
}
}
接下来就是一个它的content_item.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="horizontal"
android:padding="10dp">
<ImageView
android:id="@+id/item_imageview"
android:layout_marginLeft="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/item_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:text="aa"
android:textSize="20dp" />
</LinearLayout>
以及它的实体类:
/**
* desc: item实体类
* author: dj
* date: 2017/3/30 15:13
*/
public class ContentModel {
private int imageView;
private String text;
public ContentModel(int imageView, String text) {
super();
this.imageView = imageView;
this.text = text;
}
public int getImageView() {
return imageView;
}
public void setImageView(int imageView) {
this.imageView = imageView;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
最后就是我们绑定数据,MainActivity的代码:
/**
* desc: MainActivty
* author: dj
* date: 2017/3/30 15:17
*/
public class MainActivity extends Activity {
private DrawerLayout drawerLayout;
private List<ContentModel> list;
private ContentAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
View view = findViewById(R.id.drawer_include);
ListView listView = (ListView) view.findViewById(R.id.left_listview);
initData();
adapter = new ContentAdapter(this, list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
Toast.makeText(MainActivity.this, "你点击的" + position + 9, Toast.LENGTH_SHORT).show();
}
Toast.makeText(MainActivity.this, "你点击的" + position, Toast.LENGTH_SHORT).show();
}
});
TopBar topBar = (TopBar) findViewById(R.id.topbar);
topBar.setOnLeftAndRightClickListener(new TopBar.OnLeftAndRightClickListener() {
@Override
public void OnLeftButtonClick() {
drawerLayout.openDrawer(GravityCompat.START);
}
@Override
public void OnRightButtonClick() {
}
});
}
private void initData() {
list = new ArrayList<ContentModel>();
list.add(new ContentModel(R.drawable.doctoradvice2, "新闻"));
list.add(new ContentModel(R.drawable.infusion_selected, "订阅"));
list.add(new ContentModel(R.drawable.mypatient_selected, "图片"));
list.add(new ContentModel(R.drawable.mywork_selected, "视频"));
list.add(new ContentModel(R.drawable.nursingcareplan2, "跟帖"));
list.add(new ContentModel(R.drawable.personal_selected, "投票"));
}
}
这样我们就可以对侧滑菜单的item进行监听了,不需要用Fragment也能实现侧滑的功能,说实话很讨厌使用Fragment。
欢迎转载,但最好请注明文章原始出处。