android数据绑定< include>布局OnClick侦听器不起作用 [英] android data binding <include> layout OnClick listener doesn't work

查看:59
本文介绍了android数据绑定< include>布局OnClick侦听器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据绑定布局,其中包含一个框架布局,内部还有<include>个其他布局:

I have a data binding layout that contains a frame layout with <include> of other layout inside:

<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>

布局中的图像按钮采用以下格式:

the layout has image buttons inside in this format:

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

我在他们周围添加了标签,并添加了带有活动名称的数据

and I added the tag around them and I added data with name of activity

 <data>
    <import type="android.view.View"/>
    <variable name="listener" type="MyActivity"/>
</data>

在MyActivity中,我调用了一个函数来监听图像按钮上的单击:

and in MyActivity I called a function to listen to On click from the image buttons:

public void onClickState(View view){
int id = view.getId();
}

但是由于某些原因,当我单击时无法使用此功能 我也尝试过

but from some reason I don't get to this function when I click I tried also

 android:onClick="@{listener.onClickState}"

但没有任何帮助.

推荐答案

活动:

activityMainBinding.included.setListener(this);

global_actions.xml ::

global_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>

这篇关于android数据绑定&lt; include&gt;布局OnClick侦听器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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