【问题标题】:onClickListener doesn't trigger when using <include layout使用 <include 布局时不会触发 onClickListener
【发布时间】:2023-03-27 14:45:01
【问题描述】:

将它添加到自己的布局文件时,我的 clicklistener 不再工作。

private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    binding = DataBindingUtil.setContentView<ActivityMainBinding>(
        this, R.layout.activity_main
    ).apply {
        lifecycleOwner = this@MainActivity
    }
    binding.progressIndicatorLayout.progressIndicator.setOnClickListener {
        Log.d("","Hello")
    }
}

activity_main.xml:

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

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/rootLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <include
            android:id="@+id/progress_indicator_layout"
            layout="@layout/progress_indicator_layout" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

progress_indicator_layout:

<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
        android:id="@+id/progressIndicator"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/black"
        android:orientation="horizontal">

    </LinearLayout>
</layout>

我尝试了this solutionthis solution,但它们已经过时/不起作用。

【问题讨论】:

  • 将这些添加到progressIndicator,android:clickable="true" android:focusable="true"

标签: android kotlin onclicklistener


【解决方案1】:

我试图复制你的代码,它工作得很好。我认为您只需要清理项目并使缓存无效/重新启动即可。

如果仍然无法正常工作,请尝试检查this

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-16
    • 1970-01-01
    • 1970-01-01
    • 2013-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多