android - 从未调用过自定义 View 的 OnClick 方法 (Android)

标签 android data-binding onclick android-custom-view android-databinding

我正在尝试使用扩展 RelativeLayout 并命名为 CardView 的自定义 View 。添加了一些 attr 并为它们创建了 XML 文件,一切正常,除了 onClick 之外。因此,首先我为我的 View 创建了一个特殊的类:

class CardView(context: Context, attrs: AttributeSet) : RelativeLayout(context, attrs) {

init {
    inflate(context, R.layout.card_view, this)
    val imageView: ImageView = findViewById(R.id.image)
    val textView: TextView = findViewById(R.id.caption)
    val line: ImageView = findViewById(R.id.line)

    val attributes = context.obtainStyledAttributes(attrs, R.styleable.CardView)
    imageView.setImageResource(attributes.getResourceId(R.styleable.CardView_image, 0))
    textView.text = attributes.getString(R.styleable.CardView_text)
    line.setBackgroundColor(attributes.getColor(R.styleable.CardView_lineColor, 0))
    attributes.recycle()
} }

我的 View 也有一个 xml 文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="@dimen/cardSize"
android:layout_height="@dimen/cardSize"
android:background="@drawable/card_color_selector"
android:clickable="true">

<ImageView
    android:id="@+id/image"
    android:layout_width="60sp"
    android:layout_height="60sp"
    android:layout_centerHorizontal="true"
    android:layout_margin="3sp"
    tools:src="@color/colorPrimary" />

<TextView
    android:id="@+id/caption"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/text_normal"
    android:textSize="15dp"
    android:layout_alignParentBottom="true"
    android:layout_margin="10sp"
    android:gravity="center"
    android:textColor="@color/darkGray"
    tools:text="Caption" />

<ImageView
    android:id="@+id/line"
    android:layout_width="match_parent"
    android:visibility="visible"
    android:layout_height="5dp"
    android:layout_alignParentBottom="true"
    android:background="@color/colorPrimary" />

</RelativeLayout>

最后,我在 Activity 布局中添加了 customView ,并链接到 View 模型中的 onClick 方法,如下所示:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto">

<data>

    <variable
        name="viewModel"
        type="com.presentation.screen.HomeViewModel" />
</data>

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<com.project.presentation.utils.CardView
        android:id="@+id/card"
        android:layout_width="@dimen/cardSize"
        android:layout_height="@dimen/cardSize"
        android:clickable="true"
        android:onClick="@{() -> viewModel.onClick()}"
        app:image="@drawable/icon"
        app:lineColor="@color/colorPrimary"
        app:text="@string/text" />    

</android.support.constraint.ConstraintLayout>
</layout>

所以一切都很好,颜色按照我在 card_color_selector.xml 中编写的方式更改,但我的 ViewModel 中的方法 onClick 从未发生过变化叫。哦,顺便说一句,我的 View 模型在这里,现在我只想记录所有点击:

class HomeViewModel : BaseViewModel<Router>() {

    fun onClick() {
        Log.e("myLog", "HomeViewModel onClick")
    }
}

我已经尝试了一切!请帮助我弄清楚我做错了什么。

最佳答案

我也遇到了同样的问题。

检查您的自定义 View 的可点击属性!

如果自定义 View 中的 subview 阻止触摸事件,则您的父 View

android:onClick 将被忽略。

关于android - 从未调用过自定义 View 的 OnClick 方法 (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54531791/

相关文章:

android - 为动画 ImageView 注册 onclick

c# - 错误 CS0234 命名空间 'Xamarin.Forms.Platform 中不存在类型或命名空间名称 'Android'

android - 我无法安装英特尔 HAXM

android - 从 DialogFragment 和主 Activity 更新 RecyclerView

java - JSONException : No value for error

WPF 绑定(bind) - 通知对 ToString 值的更改

javascript - 使用 javascript 更改文本(链接)颜色 onclick

c# - 无需 INotifyPropertyChanged 即可双向数据绑定(bind)到 POCO

user-interface - Xamarin表单框架未随内容扩展

Javascript 在旧浏览器中执行点击 html "image"