Search code examples
androiddata-bindingnavigationview

Cannot detect onNavigationItemSelected using Databind. How to get all menu item click in Navigationview?


I tried this code But it did not work. In my class, I have Implemented onNavigationItemSelected.
.I have tried this https://medium.com/@oldergod/bottomnavigationview-callback-and-and-databinding-9775b0c31d38.but this does not work Dashboard layout...

<data>
    <import type="android.view.View" />
    <variable
      name="navigationItemSelectedListener"
      type="al_logistics.app.com.al_logistics.Dashboard"/>
  </data>
 <android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/header_manu"
 app:navigationItemSelectedListener="@{navigationItemSelectedListener::onNavigationItemSelected}"
    app:menu="@menu/list_menu"/>

Here is my onNavigationItemSelected

 @Override
  public boolean onNavigationItemSelected(MenuItem item) {
      Toast.makeText(this, "Clicked", Toast.LENGTH_SHORT).show();
    return true;
  }

Solution

  • Please check if you have done this.You need to implement listener otherwise it won't work.Please let me know if the solution work

    binding = DataBindingUtil.setContentView(this, R.layout.activity_dashboard);
    binding.navView.setNavigationItemSelectedListener(this);
    

    and remove this line from XML:

    app:navigationItemSelectedListener="@{navigationItemSelectedListener::onNavigationItemSelected}"