ToolBar+DrawerLayout实现左右双布局侧滑和动画返回控制显示抽屉布局

431 阅读1分钟

传统ToolBar+DrawerLayout不一样的漂移


效果图地址:
user.qzone.qq.com/2812420513/…

布局.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@android:color/background_light"
    tools:context="com.example.pc.ytf.OldDrawerActivity">

    <!--<include layout="@layout/toolbar_layout"></include>-->
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:clipToPadding="true"
        android:fitsSystemWindows="true"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:title="资讯"
        android:clickable="true"
        android:background="@android:color/holo_blue_light"
        app:titleTextColor="@android:color/darker_gray">
    </android.support.v7.widget.Toolbar>
    <android.support.v4.widget.DrawerLayout
        android:id="@+id/dl_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_orange_light">
        <!--主布局-->
        <LinearLayout
            android:layout_width="match_parent"
            android:gravity="center"
            android:layout_height="match_parent">
            <TextView
                android:id="@+id/tv_content"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="内容"
                android:textSize="25sp" />
            <ImageView
                android:id="@+id/ivRunningMan"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/profile4"/>
        </LinearLayout>


        <!--自定义listview侧滑布局-->
        <ListView
            android:id="@+id/lv_custom_layout"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:background="@android:color/holo_green_dark"
            android:fitsSystemWindows="true"
            android:layout_gravity="left" />
        <!--右侧抽屉-->
        <ListView
            android:id="@+id/lv_menu"
            android:layout_width="140dp"
            android:layout_height="match_parent"
            android:layout_gravity="right"
            android:fitsSystemWindows="true"
            android:background="@android:color/holo_orange_dark">
        </ListView>

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

</LinearLayout>

Java代码:

package com.example.pc.ytf;

import android.graphics.drawable.AnimationDrawable;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.support.v7.widget.Toolbar;

import android.view.View;

import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;

public class OldDrawerActivity extends AppCompatActivity {
    private ListView lv_custom_layout, lv_menu;//自定义抽屉布局中的list集合
    private String[] phoneprice = {"¥7188", "¥1899", "¥1099", "¥2229"};//数据源
    private String[] menus = new String[]{"item1", "item2", "item3", "item4", "item5", "item6"};
    private List list1 = new ArrayList();
    private List list2 = new ArrayList();
    private Toolbar toolbar;
    private DrawerLayout dl_layout;
    private AnimationDrawable mAnimationDrawable;
    private ActionBarDrawerToggle mDrawerToggle;
    private ImageView ivRunningMan;
    private boolean isDirection_left = false;//左侧抽屉状态的变量
    /**
     * 右边栏打开/关闭状态
     */
    private boolean isDirection_right = false;
    private View showView;//用来判断是左右哪一个抽屉布局


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_old_drawer);
        dl_layout = (DrawerLayout) findViewById(R.id.dl_layout);
//        LayoutInflater inflater=LayoutInflater.from(this);
//        View view =inflater.inflate(R.layout.toolbar_layout,null);
        ivRunningMan = (ImageView) findViewById(R.id.ivRunningMan);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setNavigationIcon(R.mipmap.ic_launcher_round);
        setSupportActionBar(toolbar);//将toolbar设置为activity的操作栏
        lv_custom_layout = (ListView) findViewById(R.id.lv_custom_layout);
        this.showView = lv_custom_layout;
        //左上角图标可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);//设置返回键可用

        //京东RunningMan动画效果,和本次Toolbar无关(主布局图片动画效果)
//        mAnimationDrawable = (AnimationDrawable) ivRunningMan.getBackground();
//        mAnimationDrawable.start();
//        实现开关事件,绑定toolbar跟drawerlayout。
        mDrawerToggle = new ActionBarDrawerToggle(this, dl_layout, toolbar, R.string.material_drawer_open, R.string.material_drawer_close) {
            @Override
            public void onDrawerOpened(View drawerView) {
//                super.onDrawerOpened(drawerView);
                isDirection_left = true;
//                mAnimationDrawable.stop();
            }

            @Override
            public void onDrawerClosed(View drawerView) {
//                super.onDrawerClosed(drawerView);
//                mAnimationDrawable.start();
                isDirection_left = false;
            }
        };
        mDrawerToggle.syncState();
        dl_layout.setDrawerListener(mDrawerToggle);



        lv_menu = (ListView) findViewById(R.id.lv_menu);
        for (int i = 0; i < phoneprice.length; i++) {
            list1.add(phoneprice[i]);
        }
        for (int i = 0; i < menus.length; i++) {
            list2.add(menus[i]);
        }
        ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list1);
        ArrayAdapter simpleAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list2);
        lv_custom_layout.setAdapter(arrayAdapter);
        lv_menu.setAdapter(simpleAdapter);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                if(view.getId()==android.R.id.home){
                    if (!isDirection_left) {
                        dl_layout.openDrawer(lv_custom_layout);
                    } else {
                        dl_layout.closeDrawer(lv_custom_layout);
                    }
//                }

            }
        });
    }
    /**
     * 加载自定义菜单
     */

//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        return super.onCreateOptionsMenu(menu);
//    }

    /**
     * 点击ActionBar上菜单
     * 部分机型无响应,所以在setNavigationOnClickListene方法代替
     */

//    @Override
//    public boolean onOptionsItemSelected(MenuItem item) {
//        if(item.getItemId()==android.R.id.home){
//            if (!isDirection_left) {
//                dl_layout.openDrawer(lv_custom_layout);
//            } else {
//                dl_layout.closeDrawer(lv_custom_layout);
//            }
//        }
        return super.onOptionsItemSelected(item);
//        return false;
//    }


}