Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
345 views
in Technique[技术] by (71.8m points)

android - ImageView with Lottie animation inside layout - problem with ClickListener

I've created my custom View class:

class CustomCheckBox constructor(
        context: Context,
        attrs: AttributeSet? = null
) : ConstraintLayout(context, attrs), Checkable {

    private var checked: Boolean = false

    init {
        LayoutInflater.from(context).inflate(R.layout.custom_view, this)
        
    }

    fun setCheckedAnim(check: Boolean) {
        isChecked = check
        animation.playAnimation()
    }

    fun toggleAnim(check: Boolean) {
        toggle(check)
        animation.playAnimation()
    }

    override fun setChecked(value: Boolean) {
        checked = value
        checkbox.isChecked = value
    }

    override fun isChecked(): Boolean = checked

    override fun toggle() {
        checked = !checked
        checkbox.isChecked = checked
    }
 
}

Layout is pretty simple:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:background="@android:color/transparent">

    <androidx.appcompat.widget.AppCompatCheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:button="@drawable/toggle_drawable"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />
    <com.airbnb.lottie.LottieAnimationView
        android:id="@+id/animation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:lottie_rawRes="@raw/animation_json"
        app:lottie_speed="2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:lottie_autoPlay="false"
        app:lottie_loop="false"/>

</androidx.constraintlayout.widget.ConstraintLayout>

The problem I am facing is that when I set ClickListener for this View in code, the Listener doesn't work. It happens because CheckBox captures every click.
If I create custom ClickListener and set is as listener for CheckBox then it works fine but the true problem is that I need this listener to be set for my main View using setOnClickListener() so the View has it, not it's component. It's because later in code I am checking if (listener != null) that's why it has to have it's own listener which will work.

question from:https://stackoverflow.com/questions/65919259/imageview-with-lottie-animation-inside-layout-problem-with-clicklistener

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...