天天看点

DrawerLayout实现侧滑菜单效果

直接上代码

package comv.tyy.drawerlayoutdemo01;

import android.app.FragmentManager;

import android.app.FragmentTransaction;

import android.graphics.Color;

import android.os.Bundle;

import android.support.v4.view.GravityCompat;

import android.support.v4.widget.DrawerLayout;

import android.support.v7.app.ActionBarActivity;

import android.support.v7.app.ActionBarDrawerToggle;

import android.support.v7.widget.Toolbar;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import fragment.FragmentA;

import fragment.FragmentB;

import fragment.FragmentC;

public class MainActivity extends ActionBarActivity {

    private ListView menuList;

    private DrawerLayout mDrawerLayout;

    private Toolbar mToolbar;

    private String[] menuListItem = {"0000000001", "000000002", "00000003"};

    private ArrayAdapter<String> maAdapter;

    private ActionBarDrawerToggle mDrawerToggle;

    private FragmentManager fragmentManager;

    private FragmentTransaction transaction;

    private FragmentA fragmentA;

    private FragmentB fragmentB;

    private FragmentC fragmentC;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        findView();

        addToolbar();

        addListener();

    }

    private void addListener() {

        fragmentManager = getFragmentManager();

        transaction = fragmentManager.beginTransaction();

        transaction.replace(R.id.mFragment, fragmentA);

        transaction.commit();

        menuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override

            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                transaction = fragmentManager.beginTransaction();

                switch (position) {

                    case 0:

                        transaction.replace(R.id.mFragment, fragmentA);

                        break;

                    case 1:

                        transaction.replace(R.id.mFragment, fragmentB);

                        break;

                    case 2:

                        transaction.replace(R.id.mFragment, fragmentC);

                        break;

                }

                transaction.commit();

                mDrawerLayout.closeDrawers();

            }

        });

    }

    private void addToolbar() {

        mToolbar.setTitle("This is Title");

        mToolbar.setTitleTextColor(Color.parseColor("#ffffff"));

        setSupportActionBar(mToolbar); //设置Toolbar

        getSupportActionBar().setHomeButtonEnabled(true); //3设置返回键可用

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//给左上角图标的左边加上一个返回的图标

//声明mDrawerToggle对象,其中R.string.open和R.string.close简单可以用"open"和"close"替代

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.open, R.string.close);

        mDrawerToggle.syncState();//实现箭头和三条杠图案切换和抽屉拉合的同步

        mDrawerLayout.addDrawerListener(mDrawerToggle);

        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {

                                                  @Override

                                                  public void onClick(View v) {

                                                      if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {

                                                          mDrawerLayout.closeDrawers();

                                                      } else {

                                                          mDrawerLayout.openDrawer(GravityCompat.START);

                                                      }

                                                  }

                                              }

        );

    }

    private void findView() {

        mToolbar = (Toolbar) findViewById(R.id.mToolbar);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.mDrawerLayout);

        menuList = (ListView) findViewById(R.id.menu_list);

        maAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_activated_1, menuListItem);

        menuList.setAdapter(maAdapter);

        fragmentA = new FragmentA();

        fragmentB = new FragmentB();

        fragmentC = new FragmentC();

    }

}

<?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:id="@+id/activity_main"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    tools:context="comv.tyy.drawerlayoutdemo01.MainActivity">

    <android.support.v7.widget.Toolbar

        android:id="@+id/mToolbar"

        android:layout_width="match_parent"

        android:layout_height="30dp"

        android:background="@color/colorPrimary">

    </android.support.v7.widget.Toolbar>

    <android.support.v4.widget.DrawerLayout

        android:id="@+id/mDrawerLayout"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:fitsSystemWindows="true">

        <!-- 主界面  -->

        <FrameLayout

            android:id="@+id/mFragment"

            android:layout_width="match_parent"

            android:layout_height="match_parent" />

        <!--侧滑菜单-->

        <LinearLayout

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            android:layout_gravity="start"

            android:background="#FFF"

            android:orientation="vertical">

            <ListView

                android:id="@+id/menu_list"

                android:layout_width="match_parent"

                android:layout_height="match_parent" />

        </LinearLayout>

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

</LinearLayout>

<!--返回键样式-->

        <item name="drawerArrowStyle">@style/ToolbarTheme</item>

    </style>

    <style name="ToolbarTheme" parent="Base.Widget.AppCompat.DrawerArrowToggle">

        <item name="color">#ffffff</item>

    </style>