Android 安卓数据绑定<;包括>;布局OnClick侦听器不';行不通
我有一个数据绑定布局,其中包含一个框架布局,其中包含其他布局的Android 安卓数据绑定<;包括>;布局OnClick侦听器不';行不通,android,android-databinding,Android,Android Databinding,我有一个数据绑定布局,其中包含一个框架布局,其中包含其他布局的: <FrameLayout android:id="@+id/global_actions_frame_layout" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.075" android:background="@color/colorToolBar"&
:
<FrameLayout
android:id="@+id/global_actions_frame_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.075"
android:background="@color/colorToolBar">
<include
android:id="@+id/included"
layout="@layout/global_actions">
</include>
</FrameLayout>
但由于某些原因,我在单击时无法使用此函数
我也试过了
android:onClick="@{listener.onClickState}"
但是没有任何帮助。如果要在包含的布局中使用
数据绑定
变量,需要将它们传递到包含的布局,并使父视图使用数据绑定
:
<layout>
<data>
<variable type="your.packages.here.MainActivity" name="listener"/>
</data>
<FrameLayout>
<include
android:id="@+id/included"
layout="@layout/global_actions"
app:listener="@{listener}"/>
</include>
</FrameLayout>
</layout>
在包含的布局中,您需要使用与父布局相同的名称(app:listener
):
请看一个类似的问题。活动:
activityMainBinding.included.setListener(this);
全局_actions.xml::
<layout>
<data>
<variable type="your.packages.here.MainActivity" name="listener"/>
</data>
<ImageButton
android:id="@+id/settingsButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_settings_black_36dp"
android:layout_gravity="end"
android:background="@color/colorToolBar"
android:layout_margin="4dp"
android:layout_marginLeft="20dp"
android:onClick="@{listener::onClickState}"
android:alpha="0.4"/>
</layout>
是否绑定包含布局变量中的数据可以通过使用应用程序命名空间和属性中的变量名从包含布局传递到包含布局的绑定中:我第一次使用包含的绑定,它工作正常,我只是包含了布局并使用了绑定。包含了\u layout.textview.setOnClickListener(..)
是的,我刚刚忘记了setListener,现在效果很好!!,对于这个问题我不能保持沉默。经过几天的研究,你救了我。谢谢:)很高兴帮助:)嗨,我不认为你也必须在家长中设置侦听器,没有它我也能工作,只有在include布局中。。在主要活动中,我添加了:mBinding.included.setListener(this);是的,这也行。但在这种情况下,您需要为您的
<layout>
<data>
<variable type="your.packages.here.MainActivity" name="listener"/>
</data>
<FrameLayout>
<include
android:id="@+id/included"
layout="@layout/global_actions"
app:listener="@{listener}"/>
</include>
</FrameLayout>
</layout>
activityMainBinding.setListener(this);
<layout>
<data>
<variable type="your.packages.here.MainActivity" name="listener"/>
</data>
<ImageButton
android:onClick="@{listener::onClickState}"/>
</layout>
activityMainBinding.included.setListener(this);
<layout>
<data>
<variable type="your.packages.here.MainActivity" name="listener"/>
</data>
<ImageButton
android:id="@+id/settingsButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_settings_black_36dp"
android:layout_gravity="end"
android:background="@color/colorToolBar"
android:layout_margin="4dp"
android:layout_marginLeft="20dp"
android:onClick="@{listener::onClickState}"
android:alpha="0.4"/>
</layout>