Android控件開發(fā)——DrawerLayout側(cè)滑菜單的實(shí)現(xiàn)

文/程序員男神

前言

精力有限棵譬,分享無限显蝌。作為一個(gè)還在學(xué)習(xí)他人代碼的碼農(nóng),深知分享的好處订咸。行走在別人博客路上的拾荒者曼尊,慢慢學(xué)會(huì)自己積累。最近有點(diǎn)忙啊脏嚷,一直在趕項(xiàng)目的路上骆撇,突然想高唱一首歌放松一下,一人我編程累......


aj

概述

先上需求圖:DrawerLayout側(cè)滑菜單的實(shí)現(xiàn)父叙。


側(cè)滑的實(shí)現(xiàn)

實(shí)現(xiàn)的功能細(xì)節(jié):

Drawerlayout是Android v4 包里自帶的神郊,既然是自帶的那么直接拿來用就可以了,當(dāng)然前提是你得工程里有 v4 包趾唱,下面解釋上面的布局文件涌乳,讓你懂得Drawerlayout用法。
首先Drawerlayout支持左劃和右劃甜癞,那它是如何控制的呢夕晓?
慢慢告訴你,以上布局分為三部分悠咱,一般情況下蒸辆,第一部分是主步局,第二部分是左劃的布局析既,第三部分是右劃的布局躬贡,其實(shí)這里的左向滑動(dòng)和右向滑動(dòng)是通過gravity控制,左劃界面android:layout_gravity="left" 當(dāng)然這里的left也可以用start代替眼坏,右劃界面就理所當(dāng)然的是android:layout_gravity="right" 拂玻,同樣right也可以用end代替,其余的應(yīng)該明白了吧!

注意:drawerlayout實(shí)現(xiàn)的側(cè)滑,側(cè)滑部分不響應(yīng)點(diǎn)擊事件纺讲,點(diǎn)擊之后只是側(cè)滑欄收進(jìn)去擂仍,button捕獲不到點(diǎn)擊事件。

解決辦法:http://bbs.csdn.net/topics/391918297
drawerlayout必須放在主界面布局后面熬甚。

實(shí)現(xiàn)步驟:

這篇文章是借著自定義TopBar內(nèi)容的基礎(chǔ)上實(shí)現(xiàn)的......
直接上代碼: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病區(qū)"
                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>

以上都是一些布局代碼逢渔,一些資源文件后面有傳送門。
接下來準(zhǔn)備我們的適配器adapter的代碼:

/**
 * desc: 側(cè)滑菜單的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;
    }
}

接下來就是一個(gè)它的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>

以及它的實(shí)體類:

/**
 * desc: item實(shí)體類
 * 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;
    }

}

最后就是我們綁定數(shù)據(jù)乡括,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, "你點(diǎn)擊的" + position + 9, Toast.LENGTH_SHORT).show();
                }
                Toast.makeText(MainActivity.this, "你點(diǎn)擊的" + 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, "投票"));
    }
}

這樣我們就可以對側(cè)滑菜單的item進(jìn)行監(jiān)聽了肃廓,不需要用Fragment也能實(shí)現(xiàn)側(cè)滑的功能,說實(shí)話很討厭使用Fragment诲泌。

歡迎轉(zhuǎn)載盲赊,但最好請注明文章原始出處。

參考文檔:http://www.mincoder.com/article/3305.shtml

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末敷扫,一起剝皮案震驚了整個(gè)濱河市哀蘑,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌葵第,老刑警劉巖绘迁,帶你破解...
    沈念sama閱讀 216,544評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異卒密,居然都是意外死亡缀台,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評論 3 392
  • 文/潘曉璐 我一進(jìn)店門哮奇,熙熙樓的掌柜王于貴愁眉苦臉地迎上來膛腐,“玉大人,你說我怎么就攤上這事鼎俘≌苌恚” “怎么了?”我有些...
    開封第一講書人閱讀 162,764評論 0 353
  • 文/不壞的土叔 我叫張陵而芥,是天一觀的道長律罢。 經(jīng)常有香客問我,道長棍丐,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,193評論 1 292
  • 正文 為了忘掉前任沧踏,我火速辦了婚禮歌逢,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘翘狱。我一直安慰自己秘案,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,216評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著阱高,像睡著了一般赚导。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上赤惊,一...
    開封第一講書人閱讀 51,182評論 1 299
  • 那天吼旧,我揣著相機(jī)與錄音,去河邊找鬼未舟。 笑死圈暗,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的裕膀。 我是一名探鬼主播员串,決...
    沈念sama閱讀 40,063評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼昼扛!你這毒婦竟也來了寸齐?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,917評論 0 274
  • 序言:老撾萬榮一對情侶失蹤抄谐,失蹤者是張志新(化名)和其女友劉穎访忿,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體斯稳,經(jīng)...
    沈念sama閱讀 45,329評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡海铆,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,543評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了挣惰。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片卧斟。...
    茶點(diǎn)故事閱讀 39,722評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖憎茂,靈堂內(nèi)的尸體忽然破棺而出珍语,到底是詐尸還是另有隱情,我是刑警寧澤竖幔,帶...
    沈念sama閱讀 35,425評論 5 343
  • 正文 年R本政府宣布板乙,位于F島的核電站,受9級特大地震影響拳氢,放射性物質(zhì)發(fā)生泄漏募逞。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,019評論 3 326
  • 文/蒙蒙 一馋评、第九天 我趴在偏房一處隱蔽的房頂上張望放接。 院中可真熱鬧,春花似錦留特、人聲如沸纠脾。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,671評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽苟蹈。三九已至糊渊,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間慧脱,已是汗流浹背渺绒。 一陣腳步聲響...
    開封第一講書人閱讀 32,825評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留磷瘤,地道東北人芒篷。 一個(gè)月前我還...
    沈念sama閱讀 47,729評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像采缚,于是被迫代替她去往敵國和親针炉。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,614評論 2 353

推薦閱讀更多精彩內(nèi)容