Android 自定义视图:如何在单个小部件上设置点击监听器

Android Custom View: How to set click listener on individual widgets

这是我的自定义视图布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

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

        <ImageView
            android:id="@+id/ivMenu"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp"
            android:background="@drawable/ripple"
            android:clickable="true"
            android:padding="12dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/ic_menu_gray" />

        <ImageView
            android:id="@+id/ivDrawer"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp"
            android:background="@drawable/ripple"
            android:clickable="true"
            android:padding="12dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/ic_drawer_gray" />
    </android.support.constraint.ConstraintLayout>
</LinearLayout>

我需要在两个 ImageView 上设置点击侦听器。我创建了一个 class 扩展 LinearLayout,因为它是我布局中的根元素。

我不知道如何为布局中的单个项目设置点击侦听器。

这是我的 class 布局文件

class WolfBottomAppBar : LinearLayout {

    constructor(context: Context) : super(context) {
        init(context)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init(context)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init(context)
    }

    private fun init(context: Context) {

        ((context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)) as LayoutInflater).inflate(R.layout.wolf_bottom_app_bar, this)

    }

    override fun dispatchTouchEvent(event: MotionEvent): Boolean {
        if (event.action == MotionEvent.ACTION_UP) {
            /*
             * I can set click listener for the whole view here, but not on individual items
             */
        }
        return super.dispatchTouchEvent(event)
    }


    override fun dispatchKeyEvent(event: KeyEvent): Boolean {
        if (event.action == KeyEvent.ACTION_UP && (event.keyCode == KeyEvent.KEYCODE_DPAD_CENTER || event.keyCode == KeyEvent.KEYCODE_ENTER)) {

        }
        return super.dispatchKeyEvent(event)
    }


}

fun setMenuListener(listener : View.OnClickListener)  {
    findViewById<ImageView>(R.id.ivMenu).setOnClickListener(listener)
}

fun setDrawerListener(listener : View.OnClickListener)  {
    findViewById<ImageView>(R.id.ivDrawer).setOnClickListener(listener)
}

进入您的自定义视图 class。

您只需使用 ImageView 的 ID 即可访问它们。您需要将膨胀视图添加到您的根目录 (=WolfBottomAppBar);例如在初始化方法中:

private fun init(context: Context) {
    ((context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)) as LayoutInflater).inflate(R.layout.wolf_bottom_app_bar, this, true)
    ivMenu.setOnClickListener { Log.d(TAG, "Image ivMenu clicked!") }
}

不要忘记为布局文件导入合成 Kotlin 包并将 android:focusable="true" 添加到两个 ImageView xml