Android控件开发——DrawerLayout侧滑菜单的实现

文/程序员男神

前言

精力有限,分享无限。作为一个还在学习他人代码的码农,深知分享的好处。行走在别人博客路上的拾荒者,慢慢学会自己积累。最近有点忙啊,一直在赶项目的路上,突然想高唱一首歌放松一下,一人我编程累......


aj

概述

先上需求图: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。

欢迎转载,但最好请注明文章原始出处。

参考文档:http://www.mincoder.com/article/3305.shtml

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容