Android数据绑定上的自定义XML属性 [英] Custom XML attribute on android databinding

查看:62
本文介绍了Android数据绑定上的自定义XML属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have used AppCompatSpinner for my fragment and I want to use setOnItemSelectedListener() in my layout. I tried to use the tutorial section from here

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

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

but it does not provide a complete example to do the simple action. And I also look for the answer from here

自定义控件中的Android数据绑定

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

and I still dun understand how to do it. I would like to have a complete example to do the simple custom binding with some attributes that's not existed in xml attribute but it is useful in the UI control

这是我的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) {
            }
        }
    }
}


推荐答案

您不需要任何特殊的操作即可分配给OnItemSelectedListener:

You don't need anything special to assign to the 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 您的布局中的变量e键入 OnItemSelectedListener

The above assumes a myItemSelectedListener variable in your layout of the type OnItemSelectedListener.

如果只想使用onItemSelected或onNothingSelected,则可以在布局已经存在:

If you want to use only the onItemSelected or onNothingSelected, you can use the attribute in your layout already:

<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}"/>

这里假定处理程序的方法类:

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

您还可以使用lambda表达式:

You can also use a lambda expression:

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

这里,处理程序的类有一个方法:

Here, handler's class has a method:

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

在所有这些情况下,您都必须分配 onCreateView 中的处理程序或侦听器,就像您使用 binding.setHandler(...)致电。您不需要调用 lAppCompatSpinner.setOnItemSelectedListener(...),因为它将作为绑定的一部分完成。

In all these cases, you must assign the handler or listener in the onCreateView, just as you're doing above with the binding.setHandler(...) call. You don't need to call lAppCompatSpinner.setOnItemSelectedListener(...) because it will be done as part of the binding.

这篇关于Android数据绑定上的自定义XML属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆