关闭

使用DrawerLayout实现侧滑菜单(两种写法都实现一样的布局)完美!

标签: DrawLayoutNavigationView
711人阅读 评论(0) 收藏 举报
分类:

此处,先奉上效果图,这个效果是使用DrawerLayout和Listview相结合实现的,是不是和DrawerLayout和NavigationView相结合的一模一样。话不多说,上代码:

主要代码部分做一下讲解,之后奉上完整小案例,

MainActivity:
package com.example.administrator.mydrawapplication;

import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

public class MainActivity extends ActionBarActivity {

    private DrawerLayout mDrawerLayout;
    private NavigationView mNavigationView;
    private ListView mLvLeftLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.id_drawer_layout);
        /*使用原生NavigationView写侧滑布局  注销listview相关内容,放开此段即为*/
//        mNavigationView = (NavigationView) findViewById(R.id.id_nv_menu);
//        Toolbar toolbar = (Toolbar) findViewById(R.id.id_toolbar);
//        setSupportActionBar(toolbar);
//        final ActionBar ab = getSupportActionBar();
//        ab.setHomeAsUpIndicator(R.mipmap.ic_launcher);
//        ab.setDisplayHomeAsUpEnabled(true);
//        setupDrawerContent(mNavigationView);
//        mNavigationView.setNavigationItemSelectedListener(
//                new NavigationView.OnNavigationItemSelectedListener() {
//                    private MenuItem mPreMenuItem;
//                    @Override
//                    public boolean onNavigationItemSelected(MenuItem menuItem) {
//                        if (mPreMenuItem != null)
//                            mPreMenuItem.setChecked(false);
//                        menuItem.setChecked(true);
//                        mDrawerLayout.closeDrawers();
//                        mPreMenuItem = menuItem;
//
//                       int id=mPreMenuItem.getItemId();
//                        switch (id){
//                            case R.id.nav_home:
//                                Toast.makeText(MainActivity.this,"主页",Toast.LENGTH_SHORT).show();
//                                break;
//                            case R.id.nav_goodscar:
//                                Toast.makeText(MainActivity.this,"购物车",Toast.LENGTH_SHORT).show();
//                                break;
//                            case R.id.nav_favrite:
//                                Toast.makeText(MainActivity.this,"我的关注",Toast.LENGTH_SHORT).show();
//                                break;
//                            case R.id.nav_brand:
//                                Toast.makeText(MainActivity.this,"品牌页",Toast.LENGTH_SHORT).show();
//                                break;
//                        }
//                        return true;
//                    }
//                });

        mDrawerLayout = (DrawerLayout) findViewById(R.id.id_drawer_layout);
        mLvLeftLayout = (ListView) findViewById(R.id.mLvLeftLayout);

        Toolbar toolbar = (Toolbar) findViewById(R.id.id_toolbar);
        setSupportActionBar(toolbar);

        final ActionBar ab = getSupportActionBar();
        ab.setHomeAsUpIndicator(R.mipmap.ic_launcher);
        ab.setDisplayHomeAsUpEnabled(true);

        LayoutInflater inflater = LayoutInflater.from(this);
        mLvLeftLayout.addHeaderView(inflater.inflate(R.layout.layout_just_username, mLvLeftLayout, false));
        mLvLeftLayout.setAdapter(new MenuItemAdapter(this));
    }

//    private void setupDrawerContent(NavigationView navigationView) {
//        navigationView.setNavigationItemSelectedListener(
//
//                new NavigationView.OnNavigationItemSelectedListener() {
//
//                    @Override
//                    public boolean onNavigationItemSelected(MenuItem menuItem) {
//                        menuItem.setChecked(true);
//                        mDrawerLayout.closeDrawers();
//                        return true;
//                    }
//                });
//    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            mDrawerLayout.openDrawer(GravityCompat.START);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}


MenuItemAdapter:
package com.example.administrator.mydrawapplication;

import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.support.v4.widget.TextViewCompat;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author zhangmanyuan
 * @date ${date}
 */
    public class MenuItemAdapter extends BaseAdapter
    {
        private final int mIconSize;
        private LayoutInflater mInflater;
        private Context mContext;

        public MenuItemAdapter(Context context)
        {
            mInflater = LayoutInflater.from(context);
            mContext = context;
            mIconSize = context.getResources().getDimensionPixelSize(R.dimen.drawer_icon_size);//24dp
        }

        private List<MenuItemMode> mItems = new ArrayList<MenuItemMode>(
                Arrays.asList(
                        new MenuItemMode(R.mipmap.nav_home, "主页"),
                        new MenuItemMode(R.mipmap.nav_car, "购物车"),
                        new MenuItemMode(R.mipmap.nav_favrite, "我的关注"),
                        new MenuItemMode(R.mipmap.nav_brand, "品牌页"),
                        new MenuItemMode(),
                        new MenuItemMode("系统设置"),
                        new MenuItemMode(R.mipmap.nav_setting, "语言设置"),
                        new MenuItemMode(R.mipmap.nav_setting, "主题设置")
                ));


        @Override
        public int getCount()
        {
            return mItems.size();
        }


        @Override
        public Object getItem(int position)
        {
            return mItems.get(position);
        }


        @Override
        public long getItemId(int position)
        {
            return position;
        }

        @Override
        public int getViewTypeCount()
        {
            //返回三种类型
            return 3;
        }

        @Override
        public int getItemViewType(int position)
        {
            return mItems.get(position).type;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            MenuItemMode item = mItems.get(position);
            switch (item.type)
            {
                case MenuItemMode.TYPE_NORMAL:
                    if (convertView == null)
                    {
                        convertView = mInflater.inflate(R.layout.design_drawer_item, parent,
                                false);
                    }
                    TextView itemView = (TextView) convertView;
                    itemView.setText(item.name);
                    Drawable icon = mContext.getResources().getDrawable(item.icon);
                    setIconColor(icon);
                    if (icon != null)
                    {
                        icon.setBounds(0, 0, mIconSize, mIconSize);
                        TextViewCompat.setCompoundDrawablesRelative(itemView, icon, null, null, null);
                    }

                    break;
                case MenuItemMode.TYPE_NO_ICON:
                    if (convertView == null)
                    {
                        convertView = mInflater.inflate(R.layout.design_drawer_item_subheader,
                                parent, false);
                    }
                    TextView subHeader = (TextView) convertView;
                    subHeader.setText(item.name);
                    break;
                case MenuItemMode.TYPE_SEPARATOR:
                    if (convertView == null)
                    {
                        convertView = mInflater.inflate(R.layout.design_drawer_item_separator,
                                parent, false);
                    }
                    break;
            }

            return convertView;
        }

        public void setIconColor(Drawable icon)
        {
            int textColorSecondary = android.R.attr.textColorSecondary;
            TypedValue value = new TypedValue();
            if (!mContext.getTheme().resolveAttribute(textColorSecondary, value, true))
            {
                return;
            }
            int baseColor = mContext.getResources().getColor(value.resourceId);
            icon.setColorFilter(baseColor, PorterDuff.Mode.MULTIPLY);
        }
    }

MenuItemMode:
package com.example.administrator.mydrawapplication;

import android.text.TextUtils;

/**
 * @author zhangmanyuan
 * @date ${date}
 */

public class MenuItemMode {

    public int type;
    public String name;
    public int icon;

    public MenuItemMode(int icon, String name) {
        this.icon = icon;
        this.name = name;

        if (icon == NO_ICON && TextUtils.isEmpty(name)) {
            type = TYPE_SEPARATOR;
        } else if (icon == NO_ICON) {
            type = TYPE_NO_ICON;
        } else {
            type = TYPE_NORMAL;
        }

        if (type != TYPE_SEPARATOR && TextUtils.isEmpty(name)) {
            throw new IllegalArgumentException("you need set a name for a non-SEPARATOR item");
        }
    }

    public MenuItemMode(String name) {
        this(NO_ICON, name);
    }

    public MenuItemMode() {
        this(null);
    }

    private static final int NO_ICON = 0;
    public static final int TYPE_NORMAL = 0;
    public static final int TYPE_NO_ICON = 1;
    public static final int TYPE_SEPARATOR = 2;


}

想看另一种想法在代码中有注释,将listview部分屏蔽,放开注释的部分即可。

布局文件就不贴出了,详情请看demo

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3725次
    • 积分:95
    • 等级:
    • 排名:千里之外
    • 原创:5篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条
    文章分类
    最新评论