我如何在数据绑定xml文件中使用getLifecycleOwner()方法

问题描述 投票:0回答:1

ViewDataBinding类提供setLifecycleOwner()setLifecycleOwner()但是当我在xml中使用它时,例如

android:onClick="@{()->InteractionPresenter.toggleFeedDiss(getLifecycleOwner())}"

将会显示出这样的错误

[数据绑定] {“ msg”:“找不到标识符\ u0027lifecycleOwner \ u0027 \ n \ n检查标识符是否拼写正确,并且没有\ u003cimport \ u003e或\ u003cvariable \ u003e标签是丢失。“,”文件“:” /用户/maxz/AndroidStudioProjects/ppjoke/app/src/main/res/layout/layout_feed_interaction.xml","pos":[{"line0":34,"col0":71 ,“ line1”:34,“ col1”:84}]}] >>

因此,如果我想在使用Lambda进行数据绑定中使用LifeCycleOwner,则必须声明另一个变量,例如

<variable
   name="lifeCycleOwner"
   type="androidx.lifecycle.LifecycleOwner" />

然后我将使用我自己的变量,它有效

但是如何通过第一种方法来获得生命周期所有者?

类ViewDataBinding提供setLifecycleOwner()和setLifecycleOwner(),但当我在android:onClick =“ @ {()-> InteractionPresenter.toggleFeedDiss(getLifecycleOwner())}等xml中使用它时,...

android kotlin android-lifecycle android-databinding android-livedata
1个回答
0
投票

要实现第一种方法,您必须将片段实例传递给布局。

<variable
    name="fragment"
    type="androidx.fragment.app.Fragment" />
© www.soinside.com 2019 - 2024. All rights reserved.