视图模型内数据绑定的BottomNavigationView

ren*_*der 5 android mvvm android-layout android-databinding bottomnavigationview

布局:

<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" />
Run Code Online (Sandbox Code Playgroud)

代码:

@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;
            }

        });

    }
Run Code Online (Sandbox Code Playgroud)

这将返回一个错误

Error:(183, 49) Could not find accessor viewmodel.onNavigationItemSelected 
Run Code Online (Sandbox Code Playgroud)

我正在尝试在我的底部导航视图上实现数据绑定

rem*_*ife 7

也许它会对某人有所帮助,通过在 xml 中设置所选项目对接受的答案进行详细补充。

绑定适配器

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

视图模型

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

使用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>
Run Code Online (Sandbox Code Playgroud)


小智 5

你应该onNavigationItemSelected()在你的viewModel班级中声明:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

在您的 xml 中,使用方法参考:

app:onNavigationItemSelected="@{viewModel::onNavigationItemSelected}"
Run Code Online (Sandbox Code Playgroud)