DrawerLayout的使用

DrawerLayout是Android官方提供的侧滑抽屉控件,通过它可以很方便的实现侧滑界面。

首先,编写工程的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.drawerviewtest.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/content_text"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#AABBCC"
                android:text="I'm content."/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="300dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="#FF99FF"
            android:layout_gravity="start" >

            <TextView
                android:id="@+id/text_a"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="#FFFFFF"
                android:layout_marginTop="4dp"
                android:text="AAAAAA" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="#FFFFFF"
                android:layout_marginTop="4dp"
                android:text="BBBBBB" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="#FFFFFF"
                android:layout_marginTop="4dp"
                android:text="CCCCCC" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="#FFFFFF"
                android:layout_marginTop="4dp"
                android:text="DDDDDD" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="#FFFFFF"
                android:layout_marginTop="4dp"
                android:text="EEEEEE" />

        </LinearLayout>



    </android.support.v4.widget.DrawerLayout>


</LinearLayout>

DrawLayout标签中应当包含至少两个子View,
第一个子View是内容视图,
第二个子View是Drawer视图。

在这里对应XML中两个LinearLayout。
对于这两个子View的属性,DrawerLayout还有以下特殊要求:

项目其他
Contentmatch_parentmatch_parent
Drawer制定宽度match_parent需要layout_gravity

现在直接运行,就可以操作侧滑菜单了,但是没有任何作用——因为我们还没有添加任何的点击事件和回调函数。

侧滑菜单状态的监听
DrawerLayout的状态可以通过其提供的回调函数监听到,我们在Activity中添加侧滑彩蛋的回调函数:

        private DrawerLayout mDrawer;

        mDrawer = (DrawerLayout) findViewById(R.id.drawer_main);

        mDrawer.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                //Log.i(TAG, "--- onDrawerSlide");
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                Log.i(TAG, "--- onDrawerOpened");
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                Log.i(TAG, "--- onDrawerClosed");
            }

            @Override
            public void onDrawerStateChanged(int newState) {
                Log.i(TAG, "--- onDrawerStateChanged");
            }
        });

在回调函数中可以监听到滑动打开关闭状态改变这些信息。

对于侧滑菜单中菜单项的点击事件,由于Drawer中的内容就是普通的布局文件,因此仍旧通过id找到View设置监听函数,或设定ListView监听函数即可。

package com.example.drawerviewtest;

import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    public static final String TAG = "MainActivity";

    private DrawerLayout mDrawer;

    private TextView textA;

    private TextView contentText;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();

    }

    public void initViews() {
        mDrawer = (DrawerLayout) findViewById(R.id.drawer_main);

        mDrawer.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                Log.i(TAG, "--- onDrawerSlide");
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                Log.i(TAG, "--- onDrawerOpened");
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                Log.i(TAG, "--- onDrawerClosed");
            }

            @Override
            public void onDrawerStateChanged(int newState) {
                Log.i(TAG, "--- onDrawerStateChanged");
            }
        });

        contentText = (TextView) findViewById(R.id.content_text);

        textA = (TextView) findViewById(R.id.text_a);
        textA.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String tmp = "item A clicked";
                Toast.makeText(MainActivity.this, tmp, Toast.LENGTH_SHORT).show();
                contentText.setText(tmp);
            }
        });
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值