NavigationView的使用笔记

2017-09-27  本文已影响198人  momo_snail

1.解决NavigationView的图片灰色

NavigationView navigationView = (NavigationView) findViewById (R.id.nv);
        //防止图片灰色 使图片显示原本的颜色
        navigationView.setItemIconTintList (null);

2.获取头布局

   //头布局
        View headerView = navigationView.getHeaderView (0);

3.头布局中文件的点击事件

 ImageView imageView = (ImageView) headerView.findViewById (R.id.iv);
        imageView.setOnClickListener (new View.OnClickListener () {
            @Override
            public void onClick(View view) {
                Toast.makeText (MainActivity.this, "我是头布局的图片", Toast.LENGTH_LONG).show ();
            }
        });

4.界面初始化的时候根据需求隐藏指定item

 //界面初始化的时候根据需求隐藏指定item
        for (int i = 0; i < navigationView.getMenu ().size (); i++) {
            int id = navigationView.getMenu ().getItem (i).getItemId ();
            switch (id) {
                case R.id.favorite:
                    Log.e (TAG, "initView: " + "favorite");
                    navigationView.getMenu ().getItem (i).setVisible (false);
                    break;
            }
        }

5.界面初始化的时候根据需求移除指定的Group

navigationView.getMenu ().removeGroup (R.id.g2);

6.设置某个Group是否可点击 false:不可点击(如果设置为false,这个Group所有的item将不可点击) true:可点击

navigationView.getMenu ().setGroupEnabled (R.id.g1, true);

7 item点击事件

7.1 item选中状态设置

7.2 设置item的文本内容

7.3 关闭导航栏的两种方法

7.4 隐藏item

//item布局
        navigationView.setNavigationItemSelectedListener (new NavigationView.OnNavigationItemSelectedListener () {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                //1.item被选中 和return true配合可以把item变成被选中状态
                item.setCheckable (true);
                switch (item.getItemId ()) {
                    case R.id.favorite:
                        break;
                    case R.id.wallet:
                        // 2.点击重新设置item的文本内容
                        item.setTitle ("我是item-wallet-2");
                        // 3.关闭导航菜单-----1. mDrawer.closeDrawers ()      2. mDrawer.closeDrawers (GravityCompat.STAR)
                        //mDrawer.closeDrawers ();
                        mDrawer.closeDrawer (GravityCompat.START);
                        break;
                    case R.id.photo:
                        break;
                    case R.id.file:
                        Toast.makeText (MainActivity.this, "我是item-file", Toast.LENGTH_LONG).show ();
                    // 4.隐藏item
                        item.setVisible (false);
                        break;
                }
                // 配合1. (item.setCheckable (true))使用
                return true;
            }
        });

全部核心代码:

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private DrawerLayout mDrawer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.main);
        initView ();
    }

    private void initView() {

        mDrawer = (DrawerLayout) findViewById (R.id.drawer);

        NavigationView navigationView = (NavigationView) findViewById (R.id.nv);
        //防止图片灰色
        navigationView.setItemIconTintList (null);

        //头布局
        View headerView = navigationView.getHeaderView (0);
        ImageView imageView = (ImageView) headerView.findViewById (R.id.iv);
        imageView.setOnClickListener (new View.OnClickListener () {
            @Override
            public void onClick(View view) {
                Toast.makeText (MainActivity.this, "我是头布局的图片", Toast.LENGTH_LONG).show ();
            }
        });


        //界面初始化的时候根据需求隐藏指定item
        for (int i = 0; i < navigationView.getMenu ().size (); i++) {
            int id = navigationView.getMenu ().getItem (i).getItemId ();
            switch (id) {
                case R.id.favorite:
                    Log.e (TAG, "initView: " + "favorite");
                    navigationView.getMenu ().getItem (i).setVisible (false);
                    break;
            }
        }
        //界面初始化的时候根据需求移除指定的Group
        navigationView.getMenu ().removeGroup (R.id.g2);

        //设置某个Group是否可点击 false:不可点击(如果设置为false,这个Group所有的item将不可点击) true:可点击
        navigationView.getMenu ().setGroupEnabled (R.id.g1, true);

        //item布局
        navigationView.setNavigationItemSelectedListener (new NavigationView.OnNavigationItemSelectedListener () {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                //item被选中 和return true配合可以把item变成被选中状态
                item.setCheckable (true);
                switch (item.getItemId ()) {
                    case R.id.favorite:
                        break;
                    case R.id.wallet:
                        //点击重新设置item的文本内容
                        item.setTitle ("我是item-wallet-2");
                        //关闭导航菜单-----1. mDrawer.closeDrawers ()      2. mDrawer.closeDrawers (GravityCompat.STAR)
                        //mDrawer.closeDrawers ();
                        mDrawer.closeDrawer (GravityCompat.START);
                        break;
                    case R.id.photo:
                        break;
                    case R.id.file:
                        Toast.makeText (MainActivity.this, "我是item-file", Toast.LENGTH_LONG).show ();
                        item.setVisible (false);
                        break;
                }
                //
                return true;
            }
        });


    }
}

demo地址

https://github.com/momosnail/NavigationViewDemo

上一篇下一篇

猜你喜欢

热点阅读