如何使用数据绑定将Click侦听器设置为LinearLayout [英] How to set click listener to LinearLayout using data binding

查看:95
本文介绍了如何使用数据绑定将Click侦听器设置为LinearLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用数据绑定将点击侦听器设置为.xml布局文件中的LinearLayout视图.

I am currently trying to set a click listener to a LinearLayout view in the .xml layout file using data binding.

我设法使它在ButtonTextView等其他视图上也能正常工作,但是由于某些原因,它不能与LinearLayout一起使用.

I have managed to get it to work well on other views like a Button or TextView, but for some reason it is not working with a LinearLayout.

这是我尝试的基本内容,但我仍然无法使它起作用:

This is the bare bones of my attempts and I still cannot get it to work:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="72dp"
    android:clickable="true"
    android:focusable="true"
    android:onClick="@{action::linearLayoutClicked}"
    android:orientation="vertical">
</LinearLayout>

其中linearLayoutClicked是我在操作类中定义的方法:

Where linearLayoutClicked is my method defined in the action class:

public void linearLayoutClicked(View view) {
    // specific logic
}

我也尝试过使用子视图,并且将clickablefocusable设置为false以及将duplicateParentState设置为truefalse的子视图.

I have also tried with child views and those child views with clickable and focusable set to false as well as duplicateParentState set to true and false.

该操作与在其他视图上正常使用的操作完全相同.

That action is exactly the same action that is being used on other views which is working correctly.

这是一个错误还是我做错了?为什么这对于LinearLayout不起作用,但是对于其他视图却没有任何问题?

Is this a bug or am I doing this incorrectly? Why is this not working for LinearLayout but works without any problems for other views?

推荐答案

因此,所有内容均已正确设置,并且所生成的绑定文件显示了正确设置的Click侦听器,但是由于某些非常奇怪的原因,一半的绑定仍在工作,并且不是新的.较新的含义是LinearLayout及其周围的所有尝试.

So everything was set up correctly and the generated bindings file shows the click listener being set up correctly, but for some very odd reason half the bindings were working and the newer ones weren't. By newer ones meaning the LinearLayout and all the attempts around it.

解决问题的方法是简单的缓存失效以及相应的重新启动和初始化,单击侦听器在LinearLayout上运行良好.只需记住将其设置为clickablefocusable,并将所有子视图设置为不可点击,这样它们就不会在父视图之前使用该事件.

What solved the problem was a simple cache invalidation and respective restart and voila, the click listener is working perfectly on the LinearLayout. Just remember to set it clickable and focusable and any child views set as not clickable so they don't consume the event before the parent.

这整整3天,花了不到10分钟的时间与其他人在评论中进行讨论,以记住试图使Android Studio的缓存无效,这让我难以置信...

3 entire days around this one, took less than 10 minutes discussing with others here in the comments to remember trying to invalidate the cache of Android Studio, unbelievable of me...

这篇关于如何使用数据绑定将Click侦听器设置为LinearLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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