单击导航不会打开活动

时间:2019-04-14 09:36:53

标签: android bottomnavigationview

我正在研究底部导航。问题是,当我单击导航栏上的一个图标时,它打开了我想要的活动,但是出现该图标时,它并没有被单击,只是默认情况下,它一直被单击时会出现第一个图标

public class MainActivity 
        extends AppCompatActivity 
        implements 
        BottomNavigationView.OnNavigationItemSelectedListener {

      BottomNavigationView nav;

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

    nav = (BottomNavigationView) findViewById(R.id.navigation);

    nav.setOnNavigationItemSelectedListener(this);
    } //  ------------------end of on create--------------------


      @Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

         switch (menuItem.getItemId()) {

        case R.id.prof:

            Intent intent = new Intent(MainActivity.this, 
            profile.class);
            menuItem.setIcon(R.drawable.profile);
            nav.setItemTextColor(ColorStateList.valueOf(Color.WHITE));
            startActivity(intent);


            break;


        case R.id.hm:
            Intent intent1 = new Intent(MainActivity.this, 
            MainActivity.class);
            nav.setItemTextColor(ColorStateList.valueOf(Color.WHITE));
            startActivity(intent1);


            break;


        case R.id.consult:
            Intent intent3 = new Intent(MainActivity.this, tabs.class);
            nav.setItemTextColor(ColorStateList.valueOf(Color.WHITE));
            startActivity(intent3);

            break;


        case R.id.dash:
            Intent intent2 = new Intent(MainActivity.this, 
            Dashboard.class);
            nav.setItemTextColor(ColorStateList.valueOf(Color.WHITE));
            startActivity(intent2);

            break;


    }
    return false;
}

}

我的主要活动布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="@color/purple"
    app:itemIconTint="@drawable/nav_item_color_state"
    app:itemTextColor="@drawable/nav_item_color_state"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/navigation" />
   </android.support.constraint.ConstraintLayout>

我的navigation.xml

<?xml version="1.0" encoding="utf-8"?> 
 <menu xmlns:tools="http://schemas.android.com/tools"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:id="@+id/hm"
    android:icon="@drawable/hom"
    android:enabled="true"
    app:showAsAction="ifRoom"
    tools:ignore="MenuTitle" />

<item
    android:id="@+id/prof"
    android:icon="@drawable/profile"
    android:enabled="true"
    app:showAsAction="ifRoom"
    tools:ignore="MenuTitle" />

 <item
    android:id="@+id/consult"
    android:icon="@drawable/consult"
    android:enabled="true"
    app:showAsAction="ifRoom"
    tools:ignore="MenuTitle" />
    <item
    android:id="@+id/dash"
    android:icon="@drawable/dashboard"
    android:enabled="true"
    app:showAsAction="ifRoom"
    tools:ignore="MenuTitle" />
  </menu>

这是我的nav_item_color_state.xml

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

</selector>

这是我所有活动的界面 enter image description here 请帮我更新问题

1 个答案:

答案 0 :(得分:1)

您不在那里绑定任何onNavigationItemSelected()侦听器;例如:

nav = (BottomNavigationView) findViewById(R.id.navigation);
nav.setNavigationItemSelectedListener(this);