layout:
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:onNavigationItemSelected="@{viewModel.onNavigationItemSelected}"
android:background="?android:attr/windowBackground"
app:menu="@menu/menu_home_tab" />
code:
@BindingAdapter("onNavigationItemSelected")
public static void setOnNavigationItemSelected(
BottomNavigationView view, BottomNavigationView listener) {
view.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_dashboard:
Log.d("test","test1");
return true;
case R.id.navigation_notifications:
Log.d("test","test2");
return true;
}
return false;
}
});
}
This returns an error
Error:(183, 49) Could not find accessor viewmodel.onNavigationItemSelected
I am trying to implement databinding on my bottomnavigationview
Maybe it will be helpful to someone, detailed addition to accepted answer with setting selected item in xml.
BindingAdapters:
public class BindingAdapters {
@BindingAdapter("onNavigationItemSelected")
public static void setOnNavigationItemSelected(
BottomNavigationView view, BottomNavigationView.OnNavigationItemSelectedListener listener) {
view.setOnNavigationItemSelectedListener(listener);
}
@BindingAdapter("selectedItemPosition")
public static void setSelectedItemPosition(
BottomNavigationView view, int position) {
view.setSelectedItemId(position);
}
}
ViewModel:
public class ViewModel implements BottomNavigationView.OnNavigationItemSelectedListener {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_favourites:
Log.d("testTag","Selected favourites");
return true;
case R.id.navigation_photos:
Log.d("testTag","Selected photos");
return true;
case R.id.navigation_info:
Log.d("testTag","Selected info");
return true;
}
return false;
}
}
Layout with BottomNavigationView:
<?xml version="1.0" encoding="utf-8"?>
<layout 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"
tools:context=".views.MainActivity">
<data>
<import
type="com.test.R"/>
<variable
name="viewModel"
type="com.test.viewmodels.ViewModel"/>
</data>
<android.support.constraint.ConstraintLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu"
app:selectedItemPosition="@{R.id.navigation_photos}"
app:onNavigationItemSelected="@{viewModel::onNavigationItemSelected}"/>
</android.support.constraint.ConstraintLayout>
</layout>
You should have onNavigationItemSelected()
declared in your viewModel
class:
class ViewModel {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_dashboard:
Log.d("test","test1");
return true;
case R.id.navigation_notifications:
Log.d("test","test2");
return true;
}
return false;
}
}
In your xml, use method reference:
app:onNavigationItemSelected="@{viewModel::onNavigationItemSelected}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With