android数据绑定上的自定义XML属性

android数据绑定上的自定义XML属性,第1张

概述我已经使用AppCompatSpinner作为我的片段,我想在我的布局中使用setOnItemSelectedListener().我试着从这里使用教程部分 https://developer.android.com/topic/libraries/data-binding/index.html?hl=en#custom_setters 但它没有提供完成简单 *** 作的完整示例.我也从这里寻找答案 an 我已经使用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"                        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:

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

上面假设在OnItemSelectedListener类型的布局中有一个myItemSelectedListener变量.

如果只想使用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}"/>

这假定了一个处理程序类的方法:

public class Handler {    public voID onItemSelected(AdapterVIEw<?> parent,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(…),因为它将作为绑定的一部分完成.

总结

以上是内存溢出为你收集整理的android数据绑定上的自定义XML属性全部内容,希望文章能够帮你解决android数据绑定上的自定义XML属性所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1122752.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存