自定义视图的OnClick方法从未调用(Android)

时间:2019-02-05 10:00:56

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

我正在尝试使用扩展了RelativeLayout并命名为CardView的自定义视图。添加了一些attr并为其创建了XML文件,除onClick之外,其他所有内容均正常运行。因此,起初我为视图创建了一个特殊的类:

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()
} }

我也为自己的视图添加了一个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>

最后,我在活动布局中添加了customView,并链接到视图模型中的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中编写的方式进行了更改,但是我的onClick中的方法ViewModel从未调用过。哦,顺便说一下,我的视图模型在这里,现在我只想记录所有点击:

class HomeViewModel : BaseViewModel<Router>() {

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

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

4 个答案:

答案 0 :(得分:0)

在您的

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

通过View在XML部分中用于

  

onClick =“方法”,您必须在方法中将参数设置为View类。

您可以看到this

答案 1 :(得分:0)

更改您的onClick方法是这样的尝试

fun onClick(view: View) {
        Log.d("myLog", "HomeViewModel onClick")
    }

答案 2 :(得分:0)

我没有足够的声誉来发表评论,所以您可以尝试

    android:onClick="@{(v) -> viewModel.onClick(v)}"

和功能

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

答案 3 :(得分:0)

我遇到了同样的问题。

检查您的customview的clickable属性!

如果您在自定义视图中的子视图阻止了触摸事件,那么您父母的

main module将被忽略。