BasicDrawerActivity

compile 'com.android.support:recyclerview-v7:26.1.0'

package com.example.zk1.demo1;

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

import com.example.zk1.R;

public class BasicDrawerActivity extends AppCompatActivity {

    private DrawerLayout drawerLayout;
    private String TAG = "BasicDrawerActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_basic_drawer);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
//      抽屉的滑动监听器
        drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
//              抽屉滑动时会被回调的方法
                Log.i(TAG, "onDrawerSlide:slideOffset====== "+slideOffset);
            }
            @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) {
//              当抽屉的状态发生改变时会回调的方法      newState:新的状态
//                0:表示关闭    1:表示打开      2:表示滑动
                Log.i(TAG, "onDrawerStateChanged: newState==="+newState);
            }
        });
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn1:
//                打开左边的抽屉
                drawerLayout.openDrawer(Gravity.LEFT);
//                关闭抽屉的方法
//                drawerLayout.closeDrawer(Gravity.LEFT);
                break;
            case R.id.btn2:
//                打开右边的抽屉
                drawerLayout.openDrawer(Gravity.RIGHT);
                break;
        }
    }
}

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawer"
    tools:context="com.example.zk1.demo1.BasicDrawerActivity">
    <!-- 界面的主内容,抽屉中间部分显示的内容-->
    <LinearLayout
        android:id="@+id/content_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <Button
            android:id="@+id/btn1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onClick"
            android:text="打开左边的抽屉"/>
        <Button
            android:id="@+id/btn2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onClick"
            android:text="打开右边的抽屉"/>
    </LinearLayout>
    <!--  第二个子View:代表抽屉  -->
    <LinearLayout
        android:id="@+id/left_drawer"
        android:layout_width="260dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_gravity="left"
        android:background="@mipmap/img02">

    </LinearLayout>

    <!-- 第三个子view:代表抽屉-->
    <LinearLayout
        android:id="@+id/right_drawer"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_gravity="right">
        <TextView
            android:id="@+id/tv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:background="@color/colorPrimary"
            android:textColor="#fff"
            android:text="我是右边的抽屉"/>
    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

package com.example.zk1;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.example.zk1.demo1.BasicDrawerActivity;
import com.example.zk1.demo2.BiliBiliActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
    }

    public void onClick(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.btn1:
                intent.setClass(this, BasicDrawerActivity.class);
                break;
            case R.id.btn2:
                intent.setClass(this, BiliBiliActivity.class);
                break;
        }
        startActivity(intent);
    }
}

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context="com.example.zk1.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="侧滑菜单:抽屉"
        android:textSize="20sp"/>
    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="抽屉的基本内容"/>
    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:textAllCaps="false"
        android:text="仿照BiliBili页面"/>

</LinearLayout>

package com.example.zk1.demo2;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.CallLog;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值