导航查看选定的菜单项经过在其他活动? [英] Navigation View Selected Menu Item Checked In Other Activity?

查看:195
本文介绍了导航查看选定的菜单项经过在其他活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的材料设计导航View..i创建菜单项,并与项目(星号标记)联活动(Activity2.java)...在该活动我延长我的主要活动,其中有导航视图使我可以从活动导航查看幻灯片太..但是当我从第二滑到活动导航视图未选中的项目,如果我preSS后退按钮并转到主要活动的previously检查菜单项显示...如何在其他activity.please帮助更新检查项目

MainActivity.java

 公共类MainActivity扩展AppCompatActivity {    //定义变量
    私人工具栏工具栏;
    私人NavigationView navigationView;
    私人DrawerLayout drawerLayout;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        //初始化工具栏,并将其设置为动作条
        工具栏=(栏)findViewById(R.id.toolbar);
        setSupportActionBar(工具栏);        //初始化NavigationView
        navigationView =(NavigationView)findViewById(R.id.navigation_view);        //设置导航查看项目中选择监听器来处理导航菜单的项目点击
        navigationView.setNavigationItemSelectedListener(新NavigationView.OnNavigationItemSelectedListener(){            //此方法将触发项目点击导航菜单
            @覆盖
            公共布尔onNavigationItemSelected(菜单项菜单项){
                //检查,如果该项目处于选中状态或没有,如果不让它在选中状态
                如果(menu​​Item.isChecked())menuItem.setChecked(假);
                其他menuItem.setChecked(真);                //项目上点击关闭抽屉
                drawerLayout.closeDrawers();                //检查已被点击哪个项目,并执行相应的操作
                开关(menu​​Item.getItemId()){
                    //更换与ContentFragment的主要内容是我们的收件箱视图;
                    案例R.id.inbox:
                        Toast.makeText(getApplicationContext(),收件箱选择Toast.LENGTH_SHORT).show();
                        ContentFragment片段=新ContentFragment();
                        android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager()调用BeginTransaction()。
                        fragmentTransaction.replace(R.id.frame,片段);
                        fragmentTransaction.commit();
                        返回true;                    //有关选项其余部分,我们只是显示上点击举杯                    案例R.id.starred:                        startActivity(新意图(MainActivity.this,Activity2.class));
                        drawerLayout.closeDrawers();
                        Toast.makeText(getApplicationContext(),怔怔的选择,Toast.LENGTH_SHORT).show();
                        返回true;
                    案例R.id.sent_mail:
                        Toast.makeText(getApplicationContext(),将选定的Toast.LENGTH_SHORT).show();
                        返回true;
                    案例R.id.drafts:
                        Toast.makeText(getApplicationContext(),草稿选,Toast.LENGTH_SHORT).show();
                        返回true;
                    案例R.id.allmail:
                        Toast.makeText(getApplicationContext(),所有邮件选择Toast.LENGTH_SHORT).show();
                        返回true;
                    案例R.id.trash:
                        Toast.makeText(getApplicationContext(),回收站选,Toast.LENGTH_SHORT).show();
                        返回true;
                    案例R.id.spam:
                        Toast.makeText(getApplicationContext(),垃圾邮件选择Toast.LENGTH_SHORT).show();
                        返回true;
                    默认:
                        Toast.makeText(getApplicationContext(),东西错了,Toast.LENGTH_SHORT).show();
                        返回true;                }
            }
        });        //初始化抽屉布局和ActionBarToggle
        drawerLayout =(DrawerLayout)findViewById(R.id.drawer);
        ActionBarDrawerToggle actionBarDrawerToggle =新ActionBarDrawerToggle(这一点,drawerLayout,工具栏,R.string.openDrawer,R.string.closeDrawer){            @覆盖
            公共无效onDrawerClosed(查看drawerView){
                一旦抽屉关闭,因为我们不希望发生什么事,所以我们离开这个空白// $ C $其中,C将被触发
                super.onDrawerClosed(drawerView);
            }            @覆盖
            公共无效onDrawerOpened(查看drawerView){
                一旦抽屉打开,因为我们不希望发生什么事,所以我们离开这个空白// $ C $其中,C将被触发                super.onDrawerOpened(drawerView);
            }
        };        //设置actionbarToggle到抽屉布局
        drawerLayout.setDrawerListener(actionBarDrawerToggle);        //调用同步状态是necessay否则你的汉堡包图标不会出现
        actionBarDrawerToggle.syncState();
    }
}

Activity2.java

 公共类活性2扩展MainActivity {    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的FrameLayout的FrameLayout =(的FrameLayout)findViewById(R.id.frame);
        //膨胀的自定义活动布局
        LayoutInflater layoutInflater =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        查看activityView = layoutInflater.inflate(R.layout.activity_2,空,假);
        //添加此活动的自定义布局框架布局。
        frameLayout.addView(activityView);
    }
}

activity_main.xml中

 < android.support.v4.widget.DrawerLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
    机器人:ID =@ + ID /抽屉
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:fitsSystemWindows =真
    工具:上下文=MainActivity。>    <的LinearLayout
        机器人:layout_height =match_parent
        机器人:layout_width =match_parent
        机器人:方向=垂直
        >
        <包括
            机器人:ID =@ + ID /工具栏
            布局=@布局/ tool_bar
        />
        <的FrameLayout
            机器人:ID =@ + ID /帧
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent>        < /&的FrameLayout GT;    < / LinearLayout中>    < android.support.design.widget.NavigationView
        机器人:ID =@ + ID / navigation_view
        机器人:layout_height =match_parent
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_gravity =开始
        应用:headerLayout =@布局/头
        应用:菜单=@菜单/抽屉
        />
< /android.support.v4.widget.DrawerLayout>

drawer.xml(menu​​_drawer)

 <?XML版本=1.0编码=UTF-8&GT?;
<菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>    <组机器人:checkableBehavior =单一>        <项目
            机器人:ID =@ + ID /收件箱
            安卓:检查=假
            机器人:图标=@绘制/ ic_inbox_black
            机器人:标题=@字符串/ inbox_string/>        <项目
            机器人:ID =@ + ID /主演的
            安卓:检查=假
            机器人:图标=@绘制/ ic_star_black
            机器人:标题=@字符串/ starred_string/>        <项目
            机器人:ID =@ + ID / sent_mail
            安卓:检查=假
            机器人:图标=@绘制/ ic_send_black
            机器人:标题=@字符串/ sent_mail_string/>        <项目
            机器人:ID =@ + ID /草稿
            安卓:检查=假
            机器人:图标=@绘制/ ic_drafts_black
            机器人:标题=@字符串/ draft_string/>
        <项目
            机器人:ID =@ + ID /寄件备份
            安卓:检查=假
            机器人:图标=@绘制/ ic_email_black
            机器人:标题=@字符串/ all_mail_string/>
        <项目
            机器人:ID =@ + ID /垃圾
            安卓:检查=假
            机器人:图标=@绘制/ ic_delete_black
            机器人:标题=@字符串/ trash_string/>
        <项目
            机器人:ID =@ + ID /垃圾邮件
            安卓:检查=假
            机器人:图标=@绘制/ ic_error_black
            机器人:标题=@字符串/ spam_string/>    < /组>
&所述; /菜单>


解决方案

  1. 添加保护INT 变量存储检查菜单项的ID

  2. 从NavigationView的clickListener检查添加菜单项的ID意向的群众演员;开始你的这个意图
  3. 第二项活动
  4. 在第二项活动获得额外的菜单项ID并将其设为类变量从(参见图1)

  5. 在你加上prepareOptionsMenu超活动

      @覆盖
    prepareOptionsMenu(菜单菜单)上公共布尔{
        super.on prepareOptionsMenu(菜单);
         //重新navigationView菜单,取消所有项目,并设定新的检查项目
        navigationView.getMenu()清()。
        navigationView.inflateMenu(R.menu.drawer);
        // setChecked(假)在NavigationView都是你的菜单项
        。navigationView.getMenu()则findItem(R.id.SOME_ID_0).setChecked(假);
        。navigationView.getMenu()则findItem(R.id.SOME_ID_1).setChecked(假);
        。navigationView.getMenu()则findItem(R.id.SOME_ID_2).setChecked(假);
        ...等等
        navigationView.setCheckedItem(checkedDrawerItemId);
     }


  6. 在你NavigationView的clickListener不要忘记单击项目的ID设置为活动类变量,并调用 supportInvalidateOptionsMenu();

i am Using Material Design Navigation View..i created Menu item And linked activity(Activity2.java) with an item(Starred)...and in that activity i extend my Main activity in which there is navigation view so that i can slide the navigation view from that activity too.. but when i slide the navigation view from the second activity the item is not checked and if i press the back button and go to the Main activity the previously checked menu item is shown...how to update the check item in other activity.please help

MainActivity.java

public class MainActivity extends AppCompatActivity {

    //Defining Variables
    private Toolbar toolbar;
    private NavigationView navigationView;
    private DrawerLayout drawerLayout;

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

        // Initializing Toolbar and setting it as the actionbar
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        //Initializing NavigationView
        navigationView = (NavigationView) findViewById(R.id.navigation_view);

        //Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

            // This method will trigger on item Click of navigation menu
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {


                //Checking if the item is in checked state or not, if not make it in checked state
                if(menuItem.isChecked()) menuItem.setChecked(false);
                else menuItem.setChecked(true);

                //Closing drawer on item click
                drawerLayout.closeDrawers();

                //Check to see which item was being clicked and perform appropriate action
                switch (menuItem.getItemId()){


                    //Replacing the main content with ContentFragment Which is our Inbox View;
                    case R.id.inbox:
                        Toast.makeText(getApplicationContext(),"Inbox Selected",Toast.LENGTH_SHORT).show();
                        ContentFragment fragment = new ContentFragment();
                        android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                        fragmentTransaction.replace(R.id.frame,fragment);
                        fragmentTransaction.commit();
                        return true;

                    // For rest of the options we just show a toast on click

                    case R.id.starred:

                        startActivity(new Intent(MainActivity.this,Activity2.class));
                        drawerLayout.closeDrawers();
                        Toast.makeText(getApplicationContext(),"Stared Selected",Toast.LENGTH_SHORT).show();
                        return true;
                    case R.id.sent_mail:
                        Toast.makeText(getApplicationContext(),"Send Selected",Toast.LENGTH_SHORT).show();
                        return true;
                    case R.id.drafts:
                        Toast.makeText(getApplicationContext(),"Drafts Selected",Toast.LENGTH_SHORT).show();
                        return true;
                    case R.id.allmail:
                        Toast.makeText(getApplicationContext(),"All Mail Selected",Toast.LENGTH_SHORT).show();
                        return true;
                    case R.id.trash:
                        Toast.makeText(getApplicationContext(),"Trash Selected",Toast.LENGTH_SHORT).show();
                        return true;
                    case R.id.spam:
                        Toast.makeText(getApplicationContext(),"Spam Selected",Toast.LENGTH_SHORT).show();
                        return true;
                    default:
                        Toast.makeText(getApplicationContext(),"Somethings Wrong",Toast.LENGTH_SHORT).show();
                        return true;

                }
            }
        });

        // Initializing Drawer Layout and ActionBarToggle
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.openDrawer, R.string.closeDrawer){

            @Override
            public void onDrawerClosed(View drawerView) {
                // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
                super.onDrawerClosed(drawerView);
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank

                super.onDrawerOpened(drawerView);
            }
        };

        //Setting the actionbarToggle to drawer layout
        drawerLayout.setDrawerListener(actionBarDrawerToggle);

        //calling sync state is necessay or else your hamburger icon wont show up
        actionBarDrawerToggle.syncState();






    }
}

Activity2.java

public class Activity2 extends MainActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FrameLayout frameLayout = (FrameLayout)findViewById(R.id.frame);
        // inflate the custom activity layout
        LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View activityView = layoutInflater.inflate(R.layout.activity_2, null,false);
        // add the custom layout of this activity to frame layout.
        frameLayout.addView(activityView);
    }
}

activity_main.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="vertical"
        >
        <include
            android:id="@+id/toolbar"
            layout="@layout/tool_bar"
        />
        <FrameLayout
            android:id="@+id/frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </FrameLayout>

    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_gravity="start"
        app:headerLayout="@layout/header"
        app:menu="@menu/drawer"
        />
</android.support.v4.widget.DrawerLayout>

drawer.xml(menu_drawer)

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group android:checkableBehavior="single">

        <item
            android:id="@+id/inbox"
            android:checked="false"
            android:icon="@drawable/ic_inbox_black"
            android:title="@string/inbox_string" />

        <item
            android:id="@+id/starred"
            android:checked="false"
            android:icon="@drawable/ic_star_black"
            android:title="@string/starred_string" />

        <item
            android:id="@+id/sent_mail"
            android:checked="false"
            android:icon="@drawable/ic_send_black"
            android:title="@string/sent_mail_string" />

        <item
            android:id="@+id/drafts"
            android:checked="false"
            android:icon="@drawable/ic_drafts_black"
            android:title="@string/draft_string" />


        <item
            android:id="@+id/allmail"
            android:checked="false"
            android:icon="@drawable/ic_email_black"
            android:title="@string/all_mail_string" />
        <item
            android:id="@+id/trash"
            android:checked="false"
            android:icon="@drawable/ic_delete_black"
            android:title="@string/trash_string" />
        <item
            android:id="@+id/spam"
            android:checked="false"
            android:icon="@drawable/ic_error_black"
            android:title="@string/spam_string" />

    </group>
</menu>

解决方案

  1. Add a protected int variable for storing id of checked menu item
  2. From NavigationView's clickListener add checked menu item id to intent's extras; start yours second Activity with this intent
  3. In second activity get menu item id from extras and set it to class variable from (see 1)
  4. add onPrepareOptionsMenu in yours super-Activity

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
         //recreate navigationView's menu, uncheck all items and set new checked item
        navigationView.getMenu().clear();
        navigationView.inflateMenu(R.menu.drawer);
        //setChecked(false) to all yours menu items in NavigationView
        navigationView.getMenu().findItem(R.id.SOME_ID_0).setChecked(false);
        navigationView.getMenu().findItem(R.id.SOME_ID_1).setChecked(false);
        navigationView.getMenu().findItem(R.id.SOME_ID_2).setChecked(false);
        ...etc
        navigationView.setCheckedItem(checkedDrawerItemId);
     }
    

  5. in yours NavigationView's clickListener do not forget to set clicked item's id to Activities class variable and call supportInvalidateOptionsMenu();

这篇关于导航查看选定的菜单项经过在其他活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆