Android Fragments 如何在单击时不更改底部导航视图项目颜色

rhfm7lfc  于 12个月前  发布在  Android
关注(0)|答案(6)|浏览(143)

我的bottomNavigationView中有三个项目

当我点击配置文件项时,代码会检查此人是否已登录。如果这个人没有登录,那么我需要启动一个新的Activity,否则我需要在我的frameLayout中加载一个fragment
现在的问题是,当我点击个人资料项目,而这个人没有登录,那么活动就开始了,但是当我点击返回时,个人资料项目被突出显示,但是主页片段被加载到框架布局中。

我尝试了以下方法来解决这个问题
1)我使用setSelectedItemId设置项目颜色时,配置文件项目被点击,但它没有工作
还有别的办法吗?

tjjdgumg

tjjdgumg1#

我终于弄明白了如何做到这一点,我在这里分享

如果不想更改项目的颜色,则需要返回false

bottomNavigationView.setOnNavigationItemSelectedListener(
            new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.action_home:
                            HomeFragment fav_frag = new HomeFragment();
                            currentFragment = fav_frag;
                            loadfragment(fav_frag);
                            break;
                        case R.id.action_designers:
                          break;
                        case R.id.action_profile:
                            if(PreferenceManager.getDefaultSharedPreferences(BaseActivity.this).getString("customer_id","").equalsIgnoreCase("")){
                                Intent intent=new Intent(BaseActivity.this,LoginActivity.class);
                                intent.putExtra("Target","Profile");
                                startActivity(intent);
                                overridePendingTransition(R.anim.slide_up,R.anim.stable);
// Here is the key , you need to return false when you don't want to change the items color
                                return false;
                            }else {
                                ProfileFragment accountFragment = new ProfileFragment();
                                currentFragment=accountFragment;
                                loadfragment(accountFragment);

                            }

                            break;
                    }
                    return true;
                }
            });
vsmadaxz

vsmadaxz2#

试试这个.

selector添加到代码中。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@android:color/holo_green_light" android:state_checked="true"/>
    <item android:color="@android:color/black" android:state_checked="false"/>
</selector>

然后添加xml代码。

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    app:itemIconTint="@drawable/selector_navigation"
    app:itemTextColor="@drawable/selector_navigation"
    app:menu="@menu/menu_navigation"/>

注意

// icon 
app:itemIconTint="@drawable/selector_navigation"
// text
app:itemTextColor="@drawable/selector_navigation"
wnavrhmk

wnavrhmk3#

你可以尝试

<android.support.design.widget.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/toolbar_background"
            app:itemIconTint="@color/bottom_nac_color"
            app:itemTextColor="@color/bottom_nac_color"
            app:menu="@menu/bottom_navigation_main" />

这里@color/bottom_nac_color是你想在屏幕上显示的颜色
希望它能起作用

qjp7pelc

qjp7pelc4#

你可以用java代码这样修改。
更新:

bottomNav.addItemNav(new ItemNav(this, R.drawable.ic_home, getResources().getString(R.string.home)).addColorAtive(R.color.yellow_selected_color).addColorInative(R.color.text_color));
7ajki6be

7ajki6be5#

return false这是因为启动活动,我们不需要突出显示菜单项

nwsw7zdq

nwsw7zdq6#

可能你在底部导航中使用了onNavigationItemSelected中的switch,并且在每个情况下都返回true。
对于每一个你不想改变颜色的项目,返回false,对于当用户点击项目时改变颜色,返回true。

bottomNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
        @SuppressLint("NonConstantResourceId")
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()){
                case R.id.one:
                    // your code
                    return true; // on click will change color
                case R.id.two:
                    // your code 
                    return false; // on click will not change color
            }
            return false;
        }
    });

相关问题