关于viewmodel内数据绑定的BottomNavigationView

时间:2017-10-22 09:56:47

标签: android android-layout mvvm 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" />

代码:

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

        });

    }

这会返回错误

Error:(183, 49) Could not find accessor viewmodel.onNavigationItemSelected 

我正在尝试在我的bottomnavigationview上实现数据绑定

4 个答案:

答案 0 :(得分:3)

对于那些在Kotlin中苦苦挣扎并且在创建绑定适配器时遇到麻烦的人,请按照以下步骤操作:

 import androidx.databinding.BindingAdapter
 import com.google.android.material.bottomnavigation.BottomNavigationView

 class BindingAdapters {
     companion object{
         @JvmStatic
         @BindingAdapter("onNavigationItemSelected")
         fun setOnNavigationItemSelectedListener(view: BottomNavigationView, listener: BottomNavigationView.OnNavigationItemSelectedListener?) {
            view.setOnNavigationItemSelectedListener(listener)
         }
     }
 }

在您的XML文件中,引用您在ViewModel中创建的方法:

<com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNavView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:layout_gravity="bottom"
            app:itemTextColor="@color/orange"
            app:onNavigationItemSelected="@{viewModel::onBottomMenuClicked}"
            app:menu="@menu/bottom_nav_menu"/>

ViewModel 类中:

fun onBottomMenuClicked(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.navigation_home->{
                ...
                return true
            }
            R.id.navigation_search->{
                ...
                return true
            }
        }
        return false
    }

答案 1 :(得分:2)

您应该在 <tr> <td><%= form.label :category_id %></td> <td><%= form.select :category_id, options_from_collection_for_select(Category.all, 'id', 'name'), :class => 'form-control',:onchange => 'addCategory(category_id)'%></td> </tr> 课程中声明<script type="text/javascript"> $('select').change(addCategory); function addCategory(category_id){ alert(category_id.value); } </script>

onNavigationItemSelected()

在xml中,使用方法参考:

viewModel

答案 2 :(得分:1)

也许可以对某人有所帮助,通过在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;
    }
}

具有 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>

答案 3 :(得分:0)

我看到的答案中没有一个让我满意,因为我希望 ViewModel 保存一个变量来反映所选的菜单项。这样我不仅可以在需要时通过视图模型以编程方式更改所选项目,还可以获取所选项目的 id。我尝试使用 InverseBindingAdapter 进行 2 路数据绑定,但有时会遇到循环更新循环。我终于找到了这个工作正常的解决方案。

@BindingAdapter("navItemSelectedHandler")
fun BottomNavigationView.setNavItemSelectedHandler(
        listener: BottomNavigationView.OnNavigationItemSelectedListener
) = setOnNavigationItemSelectedListener(listener)

@BindingAdapter("selectedMenuItemId")
fun BottomNavigationView.setSelectedMenuItemId(menuItemId: Int) {
    if (menuItemId != selectedItemId) {
        selectedItemId = menuItemId
    }
}

在视图模型中:

val selectedMenuItemId = MutableLiveData(R.id.nav_menu_home)
val navItemSelectedHanlder = BottomNavigationView.OnNavigationItemSelectedListener { item ->
    // ...
    if (selectedMenuItemId.value != item.itemId) {
        selectedMenuItemId.postValue(item.itemId)
    }
    true
}

并在 xml 中:

    app:selectedMenuItemId="@{viewModel.selectedMenuItemId}"
    app:navItemSelectedHandler="@{viewModel.navItemSelectedHanlder}"

我认为使用 InverseBindingAdapter 可能有更好的方法,但我还需要在 OnNavigationItemSelectedListener 中添加一些其他自定义处理程序。