Android开发-DesignDemo-AndroidStudio(四)单选、子菜单、header

项目压缩包下载链接:http://download.csdn.net/detail/iwanghang/9671218

Android开发-DesignDemo-AndroidStudio(四)单选、子菜单、header_第1张图片

Android开发-DesignDemo-AndroidStudio(四)单选、子菜单、header_第2张图片

Android开发-DesignDemo-AndroidStudio(四)单选、子菜单、header_第3张图片

Android开发-DesignDemo-AndroidStudio(四)单选、子菜单、header_第4张图片

activity_main.xml:




    
    

    
        
            
            
        

        

    

menu\navigation.xml:




    
    
    
    

    
        
        
        
    

    
        
            
            
            
            
            
            
            
            
        
    

drawable\header_bg.xml:


    
MainActivity.java:

package com.iwanghang.designdemo;

import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
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.MenuItem;
import android.widget.Toast;

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

/**
 * Created by iwanghang on 2016/10/31.
 */
public class MainActivity extends AppCompatActivity{
    private DrawerLayout v4_drawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ViewPager v4_viewPager = (ViewPager) findViewById(R.id.v4_viewPager);
        TabLayout design_tabLayout = (TabLayout) findViewById(R.id.design_tabLayout);
        Toolbar v7_toolbar = (Toolbar) findViewById(R.id.v7_toolbar);

        setSupportActionBar(v7_toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        v4_drawerLayout = (DrawerLayout) findViewById(R.id.v4_drawerLayout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, v4_drawerLayout, v7_toolbar, 0, 0);

        v4_drawerLayout.addDrawerListener(toggle);
        toggle.syncState();

        List list = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            list.add(String.format(Locale.CHINA,"第%02d页",i));
        }
        v4_viewPager.setAdapter(new MyAdapter(getSupportFragmentManager(),list));
        design_tabLayout.setupWithViewPager(v4_viewPager);

        NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView);
        initNavigation(navigationView); // 右滑菜单中的item点击事件监听
    }


    /**
     *  右滑菜单中的item点击事件监听
     */
    private void initNavigation(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                Toast.makeText(MainActivity.this, item + " 点击 或 选中", Toast.LENGTH_SHORT).show();
                switch (item.getItemId()){
                    case R.id.item_1: // 当点击item_1
                        finish(); // 关闭当前Activity 这里的效果是 退出App
                        break;
                    case R.id.item_2: // 当点击item_1
                        v4_drawerLayout.closeDrawer(GravityCompat.START); // 收起右滑菜单
                        break;
                    case R.id.item_3: // 当点击item_1
                        v4_drawerLayout.closeDrawer(GravityCompat.START); // 收起右滑菜单
                        break;
                    case R.id.item_4: // 当点击item_1
                        v4_drawerLayout.closeDrawer(GravityCompat.START); // 收起右滑菜单
                        break;
                }
                return true;
            }
        });
    }


}

项目压缩包下载链接:http://download.csdn.net/detail/iwanghang/9671218


你可能感兴趣的:(Android,AndroidDesign)