【问题标题】:Custom XML attribute on android databindingandroid数据绑定上的自定义XML属性
【发布时间】:2016-08-25 04:28:49
【问题描述】:

我已将 AppCompatSpinner 用于我的片段,我想在我的布局中使用 setOnItemSelectedListener()。我尝试使用此处的教程部分

https://developer.android.com/topic/libraries/data-binding/index.html?hl=en#custom_setters

但它没有提供一个完整的示例来执行简单的操作。我也从这里寻找答案

android databinding in custom controls

我仍然不明白该怎么做。 我想要一个完整的例子来做一些简单的自定义绑定,这些属性在 xml 属性中不存在,但在 UI 控件中很有用

这是我的 xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:apps="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
    >

    <data>

        <import type="android.view.View"/>

        <variable
            name="handler"
            type="com.my.OldHandlerInterface"/>
    </data>

    <merge
        tools:showIn="@layout/fragment_stock_replacement">


        <android.support.v7.widget.CardView
            android:id="@+id/exist_eqpt_card"
            style="@style/sccardview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.29"
            android:visibility="@{oldObj.updateOld_mode ? View.VISIBLE : View.GONE}"
            >

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:orientation="vertical">

                <android.support.v7.widget.AppCompatSpinner
                    android:id="@+id/spn_status"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_below="@+id/chk_installed"
                    apps:adapter="@{statusAdapter}"/>
            </RelativeLayout>

        </android.support.v7.widget.CardView>
        <!--</LinearLayout>-->

    </merge>
</layout>

这是我的片段

public class ReplacementFragment extends QRScanFragment {
    ../
    @BindingAdapter("app:setOnItemSelectedListener")
    public static void setOnItemSelectedListener(AppCompatSpinner view, int pos) {
        //do sth
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.binding, container, false);
        String[] status = new String[]{"Spare", "Lost", "Damage", "Faulty"};
        statusAdapter = new StatusAdapter(getActivity(), status);
        binding.setHandler(new Handler());
        View view = binding.getRoot();
        AppCompatSpinner lAppCompatSpinner = (AppCompatSpinner) view.findViewById(R.id.spn_status);
        lAppCompatSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
         @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            }
        }
    }
}

【问题讨论】:

    标签: android data-binding


    【解决方案1】:

    您不需要任何特殊的东西来分配给 OnItemSelectedListener:

    <android.support.v7.widget.AppCompatSpinner
        android:id="@+id/spn_status"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/chk_installed"
        android:onItemSelectedListener="@{myItemSelectedListener}"
        apps:adapter="@{statusAdapter}"/>
    

    以上假设您的布局中有一个myItemSelectedListener 变量OnItemSelectedListener

    如果您只想使用 onItemSelected 或 onNothingSelected,您可以在布局中使用该属性:

    <android.support.v7.widget.AppCompatSpinner
        android:id="@+id/spn_status"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/chk_installed"
        android:onItemSelected="@{handler::onItemSelected}"
        apps:adapter="@{statusAdapter}"/>
    

    这假定handler 的类上有一个方法:

    public class Handler {
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            //...
        }
    }
    

    您也可以使用 lambda 表达式:

    android:onItemSelected="@{(p, v, pos, id) -> handler.onItemSelected(v, pos)}"
    

    这里,handler的类有一个方法:

    public class Handler {
        public void onItemSelected(View view, int position) {
            //...
        }
    }
    

    在所有这些情况下,您必须在 onCreateView 中分配处理程序或侦听器,就像您在上面对 binding.setHandler(...) 调用所做的那样。您无需调用lAppCompatSpinner.setOnItemSelectedListener(...),因为它将作为绑定的一部分完成。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-19
      • 1970-01-01
      • 2020-10-21
      • 2018-09-03
      • 1970-01-01
      相关资源
      最近更新 更多