Android Design Support Library(二)用NavigationView实现抽屉菜单界面

NavigationView在MD设计中非常重要,之前Google也提出了使用DrawerLayout来实现导航抽屉。这次,在Android Design Support Library中,Google提供了NavigationView来实现导航菜单界面。

这次我们写的代码在Android用TabLayout实现类似网易选项卡动态滑动效果这篇文章代码的基础上进行修改,所以最好先看看上面这篇文章

首先仍旧是配置build.gradle:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.android.support:design:22.2.0'
    compile 'com.android.support:recyclerview-v7:22.2.0'
    compile 'com.android.support:cardview-v7:22.2.0'
}

'com.android.support:design:22.2.0'com.android.support:design:22.2.0就是我们需要引入的Android Design Support Library,其次我们还引入了Recyclerview和Cardview。

主界面布局(activity_tab_layout.xml)






    
        

        

        

    



    

DrawerLayout标签包含了主界面的布局以及抽屉的布局,NavigationView标签下app:headerLayout="" 可以引入头部文件
app:menu=""则引入菜单的布局。

头部布局文件(navigation_header.xml)




    

    


菜单布局文件(drawer_view.xml)





    
        
        
        
        
    

    
        
            
            
        
    



来看看主界面的java代码(TabLayoutActivity.java)

package com.example.liuwangshu.mytablayout;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

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

public class TabLayoutActivity extends AppCompatActivity {
    private DrawerLayout mDrawerLayout;
    private ViewPager mViewPager;
    private TabLayout mTabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_layout);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        final ActionBar ab = getSupportActionBar();
        ab.setHomeAsUpIndicator(R.drawable.ic_menu);
        ab.setDisplayHomeAsUpEnabled(true);
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.dl_main_drawer);
        NavigationView navigationView =
                (NavigationView) findViewById(R.id.nv_main_navigation);
        if (navigationView != null) {
            navigationView.setNavigationItemSelectedListener(
                    new NavigationView.OnNavigationItemSelectedListener() {
                        @Override
                        public boolean onNavigationItemSelected(MenuItem menuItem) {
                            menuItem.setChecked(true);
                            mDrawerLayout.closeDrawers();
                            return true;
                        }
                    });
        }
        initViewPager();
    }

navigationView.setNavigationItemSelectedListener对菜单的条目进行了监听,如果被点击则将条目设置为选中状态并收回抽屉。当然别忘了对toolbar的菜单选项进行监听回调,否则抽屉就出不来了。
(TabLayoutActivity.java)

  @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                mDrawerLayout.openDrawer(GravityCompat.START);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

最后运行程序来看看效果


Android Design Support Library(二)用NavigationView实现抽屉菜单界面_第1张图片

github源码下载

你可能感兴趣的:(Android Design Support Library(二)用NavigationView实现抽屉菜单界面)