【问题标题】:OnClick method for a custom view has never called (Android)自定义视图的 OnClick 方法从未调用过 (Android)
【发布时间】:2019-02-05 10:00:56
【问题描述】:

我正在尝试使用扩展 RelativeLayout 并命名为 CardView 的自定义视图。 A 添加了一些 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 中写的那样,但是我的ViewModel 中的方法onClick 从未调用过。哦,顺便说一句,我的视图模型在这里,现在我只想记录所有点击:

class HomeViewModel : BaseViewModel<Router>() {

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

我什么都试过了!请帮我弄清楚我做错了什么。

【问题讨论】:

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


    【解决方案1】:

    我遇到了同样的问题。

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

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

    android:onClick 将被忽略。

    【讨论】:

    • 这个答案被低估了。它解决了我的问题。我已经担心了好几天了
    【解决方案2】:

    改变你的onClick方法像这样试试这样

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

    【讨论】:

    • 不幸的是,我已经尝试过这种方式,它不会改变任何东西:(
    • 如果你不介意我可以问你一件事为什么你使用Log.e而不是Log.d或其他?您是否正确检查了日志??
    • 是的,因为我的应用程序中还有另一个日志,所以我对它们没有任何问题。
    • 看起来我必须将 onClickListener 设置为我的自定义视图,但在 mvvm 架构中我不能使用像 findViewById 这样的东西。这就是为什么我正在寻找另一个解决方案。
    【解决方案3】:

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

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

    对于函数

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

    【讨论】:

    • 是的,我已经尝试过了,但我不知道为什么它不起作用。问题是否与我的自定义视图中的布局属性 android:clickable="true" 相关?
    • 可能是,唯一知道的方法就是测试它。删除两个可点击的真实属性并对其进行测试。并确保您设置了 homeViewModel 的值。
    【解决方案4】:

    在你的

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

    通过 View 在 XML 部分中为

    onClick="method" 您必须在方法中将参数设置为View 类。

    你可以看到this

    【讨论】:

      猜你喜欢
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 2015-04-10
      • 2023-04-02
      • 1970-01-01
      • 2013-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多