代码之家  ›  专栏  ›  技术社区  ›  BRDroid

从spinner-MVVM-Databinding获取所选项目

  •  0
  • BRDroid  · 技术社区  · 4 年前

    这是我的微调器数据是从数组列表加载的

        <data class=".AddProductBinding">
            <variable
                name="addProductViewModel"
                type="com.rao.iremind.AddProductViewModel" />
        </data>
    
      <Spinner
                android:id="@+id/spinner_catagory"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:entries="@array/product_catagory"
                app:layout_constraintTop_toBottomOf="@+id/label_catagory" />
    

    如何在视图模型中获取所选项目

    0 回复  |  直到 4 年前
        1
  •  0
  •   Róbert Nagy    4 年前

    AFAIK Spinner没有为您的用例编写绑定适配器。所以你有两个选择:

    1. 创建一个简单的 Binding Adapter 为了你的听众
     @BindingAdapter("onItemSelected")
     fun Spinner.setItemSelectedListener(itemSelectedListener: (T) -> Unit) {
            setOnItemClickListener{ _, view, _, _ ->
               itemSelectedListener(...)
        }
    }
    

    然后从数据绑定

    app:onItemSelected="@{(value) -> addProductViewModel.onItemSelected(value))}" />
    
    1. 建立一个 Inverse Binding Adapter docs