From 5678d82fc5213f15b7efe13dafd1a5ee68d01645 Mon Sep 17 00:00:00 2001 From: skydoves Date: Wed, 29 Jul 2020 00:01:50 +0900 Subject: [PATCH] Add OnBalloonInitializedListener for listening initialized balloon content --- .../main/java/com/skydoves/balloon/Balloon.kt | 20 ++++++++++++++ .../balloon/OnBalloonInitializedListener.kt | 26 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 balloon/src/main/java/com/skydoves/balloon/OnBalloonInitializedListener.kt diff --git a/balloon/src/main/java/com/skydoves/balloon/Balloon.kt b/balloon/src/main/java/com/skydoves/balloon/Balloon.kt index 1670ddd2..1dfff048 100644 --- a/balloon/src/main/java/com/skydoves/balloon/Balloon.kt +++ b/balloon/src/main/java/com/skydoves/balloon/Balloon.kt @@ -90,6 +90,7 @@ class Balloon( private var destroyed: Boolean = false var onBalloonClickListener: OnBalloonClickListener? = null var onBalloonDismissListener: OnBalloonDismissListener? = null + var onBalloonInitializedListener: OnBalloonInitializedListener? = null var onBalloonOutsideTouchListener: OnBalloonOutsideTouchListener? = null private var supportRtlLayoutFactor: Int = LTR.unaryMinus(builder.isRtlSupport) private val balloonPersistence = BalloonPersistence.getInstance(context) @@ -151,6 +152,7 @@ class Balloon( } binding.root.post { binding.balloonArrow.visible(builder.arrowVisible) + onBalloonInitializedListener?.onBalloonInitialized(getContentView()) when (builder.arrowOrientation) { ArrowOrientation.BOTTOM, ArrowOrientation.TOP -> { x = getArrowConstraintPositionX(anchor) @@ -266,6 +268,7 @@ class Balloon( private fun initializeBalloonListeners() { this.onBalloonClickListener = builder.onBalloonClickListener this.onBalloonDismissListener = builder.onBalloonDismissListener + this.onBalloonInitializedListener = builder.onBalloonInitializedListener this.onBalloonOutsideTouchListener = builder.onBalloonOutsideTouchListener this.binding.root.setOnClickListener { this.onBalloonClickListener?.onBalloonClick(it) @@ -867,6 +870,9 @@ class Balloon( @JvmField var onBalloonDismissListener: OnBalloonDismissListener? = null + @JvmField + var onBalloonInitializedListener: OnBalloonInitializedListener? = null + @JvmField var onBalloonOutsideTouchListener: OnBalloonOutsideTouchListener? = null @@ -1216,6 +1222,11 @@ class Balloon( this.onBalloonDismissListener = value } + /** sets a [OnBalloonInitializedListener] to the popup. */ + fun setOnBalloonInitializedListener(value: OnBalloonInitializedListener): Builder = apply { + this.onBalloonInitializedListener = value + } + /** sets a [OnBalloonOutsideTouchListener] to the popup. */ fun setOnBalloonOutsideTouchListener(value: OnBalloonOutsideTouchListener): Builder = apply { this.onBalloonOutsideTouchListener = value @@ -1239,6 +1250,15 @@ class Balloon( } } + /** sets a [OnBalloonInitializedListener] to the popup using lambda. */ + fun setOnBalloonInitializedListener(unit: (View) -> Unit): Builder = apply { + this.onBalloonInitializedListener = object : OnBalloonInitializedListener { + override fun onBalloonInitialized(contentView: View) { + unit(contentView) + } + } + } + /** sets a [OnBalloonOutsideTouchListener] to the popup using lambda. */ fun setOnBalloonOutsideTouchListener(unit: (View, MotionEvent) -> Unit): Builder = apply { this.onBalloonOutsideTouchListener = object : OnBalloonOutsideTouchListener { diff --git a/balloon/src/main/java/com/skydoves/balloon/OnBalloonInitializedListener.kt b/balloon/src/main/java/com/skydoves/balloon/OnBalloonInitializedListener.kt new file mode 100644 index 00000000..70943bad --- /dev/null +++ b/balloon/src/main/java/com/skydoves/balloon/OnBalloonInitializedListener.kt @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2019 skydoves + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.skydoves.balloon + +import android.view.View + +/** Interface definition for a callback to be invoked when a balloon view is initialized. */ +interface OnBalloonInitializedListener { + + /** invoked when the [Balloon] is initialized. */ + fun onBalloonInitialized(contentView: View) +}