自定义视图OnClick方法从来没有所谓的(安卓)

问题描述 投票:0回答:3

我试图使用扩展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")
    }
}

我用尽了一切!请帮我找出我在做什么错。

android data-binding onclick android-custom-view android-databinding
3个回答
0
投票

更改qazxsw POI方法类似thisTry这样

onClick

0
投票

我没有足够的信誉发表评论,所以你可以尝试

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

和功能

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

0
投票

在你的

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

通过fun onClick() { Log.e("myLog", "HomeViewModel onClick") } 在用于XML部分

的onClick =“方法”,你必须设定的参数作为View类的方法。

你可以看到View


0
投票

我有同样的问题。

检查customview的可点击的属性!

如果自定义视图中的子视图是防止触摸事件,你父母的

this将被忽略。

© www.soinside.com 2019 - 2024. All rights reserved.