【问题标题】:Android Custom View: How to set click listener on individual widgetsAndroid 自定义视图:如何在单个小部件上设置点击监听器
【发布时间】:2018-07-20 10:11:21
【问题描述】:

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

<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 上设置点击侦听器。我创建了一个扩展 LinearLayout 的类,因为它是我布局中的根元素。

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

这是我的布局类文件

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


}

【问题讨论】:

    标签: android xml user-interface custom-view


    【解决方案1】:

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

    进入你的自定义视图类。

    【讨论】:

    • fun setMenuListener(listener: View.OnClickListener) { findViewById(R.id.ivMenu).setOnClickListener(listener) } fun setDrawerListener(listener: View.OnClickListener) { findViewById( R.id.ivDrawer).setOnClickListener(listener) } 这有帮助:)
    【解决方案2】:

    您可以使用他们的 ID 访问 ImageViews。您需要将膨胀视图添加到您的根目录 (=WolfBottomAppBar);例如在 init 方法中:

    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" 添加到两个 ImageViews xml 中

    【讨论】:

      猜你喜欢
      • 2019-11-04
      • 1970-01-01
      • 2020-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多