底部导航菜单onClick图标选择 [英] Bottom Navigation Menu onClick icon select

查看:91
本文介绍了底部导航菜单onClick图标选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为有两个相关的问题.背景:我有三个底部的导航项,它们会导致其他布局和活动.其中之一是家.

Two problems which I think are related. Background: I have three bottom navigation items which lead to other layouts and activities; one of which is Home.

  1. 从主页开始执行onClick时,将打开选定的活动和布局,但是选定的图标仅在单击时突出显示,然后突出显示主页".如果再次按下,它将突出显示所选的布局图标.如何使首次点击时突出显示图标?

  1. From Home, when onClick is performed, selected activity and layout opens up, yet the selected icon only highlights on click and then highlights Home. If pressed again, then it highlights selected layout icon. How do I make the icon highlighted on first click?

主页上还有其他三个按钮可以引导您进行其他活动.选择主页"后,要花费很长的时间才能返回主页".为什么?其他活动/布局目前为空.我需要在后台运行服务吗?Home Java/Activity代码并不长,但是返回它需要很长时间.

Home has three other buttons that lead to other activities. When Home is selected, it takes considerably long time to return Home. Why is that so? Other activities/layouts are empty for now. Do I need to run a service in the background? Home Java/Activity code isn't that long, yet it takes a long time to return to.

真的很感谢您的一些想法.也许答案是零散的,不确定是否可以解决第一个问题?我已经包含了用于从首页进行底部导航的代码.问候,爱德华

Would really appreciate some thoughts. Maybe the answer is in fragments, not sure if that will solve the first problem though? I have included my code for bottom navigation from Home. Regards, Edward

P.S.我一个月前才开始使用Java和Android Studio.

P.S. I have only started working with Java and Android Studio a month ago.

    //Declare navigation view ID (bottomnav_view) in content_main
    BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomnav_view);
    bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()){
                case R.id.ic_home: break;
                case R.id.ic_instruments:
                    Intent intentinstruments = new Intent(MainActivity.this, InstrumentListActivity.class);
                    startActivity(intentinstruments);
                    break;
                case R.id.ic_methods:
                    Intent intentmethods = new Intent(MainActivity.this, MethodsActivity.class);
                    startActivity(intentmethods);
                    break;
            }
            return true;
        }
    });
    //End of bottom hav handler

推荐答案

在实现时始终首选片段BottomNavigationView.

Fragments are always preferred while implementing BottomNavigationView.

尝试这种方式.

bottomNavigationView.setOnNavigationItemSelectedListener(new  
    BottomNavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment selectedFragment = null;

        switch (item.getItemId()){
            case R.id.navigation_News:
                selectedFragment = ItemoneFragment.newInstance();
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.content,selectedFragment);
                transaction.addToBackStack(null);
                transaction.commit();
                return true;

            case R.id.navigation_profile:
                selectedFragment = ItemtwoFragment.newInstance();
                transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.content,selectedFragment);
                transaction.addToBackStack(null);
                transaction.commit();
                return true;

        }
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.content, selectedFragment);
        transaction.commit();
        return true;
    }
});

这篇关于底部导航菜单onClick图标选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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