Skip to content

Commit

Permalink
Added "setOnDismissListener" to be notified when the popup menu closes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Zawadzki authored and zawadz88 committed Dec 21, 2018
1 parent 3064401 commit e02c549
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 44 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [3.1.0]
### Added
- A listener to get notified when popup menus get dismissed

## [3.0.0]
### Changed
- Migrated to AndroidX package structure (https://developer.android.com/topic/libraries/support-library/androidx-overview)
Expand Down Expand Up @@ -62,6 +66,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Added
Initial release of the library.

[3.1.0]: https://github.com/zawadz88/MaterialPopupMenu/compare/3.0.0...3.1.0
[3.0.0]: https://github.com/zawadz88/MaterialPopupMenu/compare/2.2.0...3.0.0
[2.2.0]: https://github.com/zawadz88/MaterialPopupMenu/compare/2.1.0...2.2.0
[2.1.0]: https://github.com/zawadz88/MaterialPopupMenu/compare/2.0.0...2.1.0
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This library allows to create simple popup menus programmatically with a nice [t

## Download (from JCenter)
```groovy
implementation 'com.github.zawadz88.materialpopupmenu:material-popup-menu:3.0.0'
implementation 'com.github.zawadz88.materialpopupmenu:material-popup-menu:3.1.0'
```

## Getting started
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

buildscript {
ext.KOTLIN_VERSION = '1.2.71'
ext.ANDROID_GRADLE_PLUGIN_VERSION = '3.2.0'
ext.ANDROID_GRADLE_PLUGIN_VERSION = '3.2.1'
ext.DOKKA_VERSION = '0.9.17'
ext.BINTRAY_VERSION = '1.7.3'
repositories {
Expand Down Expand Up @@ -32,7 +32,7 @@ configure(allprojects) {
ANDROID_MIN_SDK_VERSION = 16
ANDROID_TARGET_SDK_VERSION = 28
ANDROID_COMPILE_SDK_VERSION = 28
ANDROID_BUILD_TOOLS_VERSION = '28.0.2'
ANDROID_BUILD_TOOLS_VERSION = '28.0.3'

ANDROIDX_CORE_VERSION = '1.0.0'
ANDROIDX_ANNOTATION_VERSION = '1.0.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ <h3>Functions</h3>
</tr>
<tr>
<td>
<p><a href="set-on-dismiss-listener.html">setOnDismissListener</a></p>
</td>
<td>
<code><span class="keyword">fun </span><span class="identifier">setOnDismissListener</span><span class="symbol">(</span><span class="identifier" id="com.github.zawadz88.materialpopupmenu.MaterialPopupMenu$setOnDismissListener(kotlin.Function0((kotlin.Unit)))/listener">listener</span><span class="symbol">:</span>&nbsp;<span class="symbol">(</span><span class="symbol">)</span>&nbsp;<span class="symbol">-&gt;</span>&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html"><span class="identifier">Unit</span></a><span class="symbol">)</span><span class="symbol">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html"><span class="identifier">Unit</span></a></code>
<p>Sets a listener that is called when this popup window is dismissed.</p>
</td>
</tr>
<tr>
<td>
<p><a href="show.html">show</a></p>
</td>
<td>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<HTML>
<HEAD>
<meta charset="UTF-8">
<title>MaterialPopupMenu.setOnDismissListener - material-popup-menu</title>
<link rel="stylesheet" href="../../../style.css">
</HEAD>
<BODY>
<a href="../../index.html">material-popup-menu</a>&nbsp;/&nbsp;<a href="../index.html">com.github.zawadz88.materialpopupmenu</a>&nbsp;/&nbsp;<a href="index.html">MaterialPopupMenu</a>&nbsp;/&nbsp;<a href="./set-on-dismiss-listener.html">setOnDismissListener</a><br/>
<br/>
<h1>setOnDismissListener</h1>
<a name="com.github.zawadz88.materialpopupmenu.MaterialPopupMenu$setOnDismissListener(kotlin.Function0((kotlin.Unit)))"></a>
<code><span class="keyword">fun </span><span class="identifier">setOnDismissListener</span><span class="symbol">(</span><span class="identifier" id="com.github.zawadz88.materialpopupmenu.MaterialPopupMenu$setOnDismissListener(kotlin.Function0((kotlin.Unit)))/listener">listener</span><span class="symbol">:</span>&nbsp;<span class="symbol">(</span><span class="symbol">)</span>&nbsp;<span class="symbol">-&gt;</span>&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html"><span class="identifier">Unit</span></a><span class="symbol">)</span><span class="symbol">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html"><span class="identifier">Unit</span></a></code>
<p>Sets a listener that is called when this popup window is dismissed.</p>
<h3>Parameters</h3>
<p><a name="listener"></a>
<code>listener</code> - Listener that is called when this popup window is dismissed.</p>
</BODY>
</HTML>
2 changes: 1 addition & 1 deletion material-popup-menu/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ ext {
siteUrl = 'https://github.com/zawadz88/MaterialPopupMenu'
gitUrl = 'https://github.com/zawadz88/MaterialPopupMenu.git'

libraryVersion = '3.0.0'
libraryVersion = '3.1.0'

developerId = 'zawadz88'
developerName = 'Piotr Zawadzki'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ import java.lang.reflect.Method
*/
@SuppressLint("PrivateResource,RestrictedApi")
class MaterialRecyclerViewPopupWindow(
context: Context,
private var dropDownGravity: Int,
@StyleRes defStyleRes: Int) {
context: Context,
private var dropDownGravity: Int,
@StyleRes defStyleRes: Int
) {

companion object {

Expand All @@ -43,18 +44,19 @@ class MaterialRecyclerViewPopupWindow(
init {
try {
sClipToWindowEnabledMethod = PopupWindow::class.java.getDeclaredMethod(
"setClipToScreenEnabled", Boolean::class.javaPrimitiveType)
"setClipToScreenEnabled", Boolean::class.javaPrimitiveType
)
} catch (e: NoSuchMethodException) {
Log.i(TAG, "Could not find method setClipToScreenEnabled() on PopupWindow. Oh well.")
}

try {
sGetMaxAvailableHeightMethod = PopupWindow::class.java.getDeclaredMethod(
"getMaxAvailableHeight", View::class.java, Int::class.javaPrimitiveType, Boolean::class.javaPrimitiveType)
"getMaxAvailableHeight", View::class.java, Int::class.javaPrimitiveType, Boolean::class.javaPrimitiveType
)
} catch (e: NoSuchMethodException) {
Log.i(TAG, "Could not find method getMaxAvailableHeight(View, int, boolean)" + " on PopupWindow. Oh well.")
}

}
}

Expand Down Expand Up @@ -138,9 +140,10 @@ class MaterialRecyclerViewPopupWindow(

popup.isOutsideTouchable = true

popup.update(anchorView, dropDownHorizontalOffset,
dropDownVerticalOffset, widthSpec,
if (height < 0) -1 else height
popup.update(
anchorView, dropDownHorizontalOffset,
dropDownVerticalOffset, widthSpec,
if (height < 0) -1 else height
)
} else {
popup.width = widthSpec
Expand All @@ -150,8 +153,10 @@ class MaterialRecyclerViewPopupWindow(
// use outside touchable to dismiss drop down when touching outside of it, so
// only set this if the dropdown is not always visible
popup.isOutsideTouchable = true
PopupWindowCompat.showAsDropDown(popup, anchorView!!, dropDownHorizontalOffset,
dropDownVerticalOffset, dropDownGravity)
PopupWindowCompat.showAsDropDown(
popup, anchorView!!, dropDownHorizontalOffset,
dropDownVerticalOffset, dropDownGravity
)
}
}

Expand All @@ -163,6 +168,19 @@ class MaterialRecyclerViewPopupWindow(
popup.contentView = null
}

/**
* Sets a listener that is called when this popup window is dismissed.
*
* @param listener Listener that is called when this popup window is dismissed.
*/
fun setOnDismissListener(listener: (() -> Unit)?) {
if (listener != null) {
popup.setOnDismissListener { listener.invoke() }
} else {
popup.setOnDismissListener(null)
}
}

/**
*
* Builds the popupMenu window's content and returns the height the popupMenu
Expand All @@ -181,7 +199,6 @@ class MaterialRecyclerViewPopupWindow(

popup.contentView = dropDownList


// getMaxAvailableHeight() subtracts the padding, so we put it back
// to get the available height for the whole window.
val padding: Int
Expand All @@ -204,8 +221,10 @@ class MaterialRecyclerViewPopupWindow(

// Max height available on the screen for a popupMenu.
val ignoreBottomDecorations = popup.inputMethodMode == PopupWindow.INPUT_METHOD_NOT_NEEDED
val maxHeight = getMaxAvailableHeight(anchorView!!, dropDownVerticalOffset,
ignoreBottomDecorations)
val maxHeight = getMaxAvailableHeight(
anchorView!!, dropDownVerticalOffset,
ignoreBottomDecorations
)

val listContent = measureHeightOfChildrenCompat(maxHeight - otherHeights)
if (listContent > 0) {
Expand Down Expand Up @@ -255,8 +274,10 @@ class MaterialRecyclerViewPopupWindow(
}

heightMeasureSpec = if (childLp.height > 0) {
View.MeasureSpec.makeMeasureSpec(childLp.height,
View.MeasureSpec.EXACTLY)
View.MeasureSpec.makeMeasureSpec(
childLp.height,
View.MeasureSpec.EXACTLY
)
} else {
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
}
Expand All @@ -277,8 +298,10 @@ class MaterialRecyclerViewPopupWindow(
}

private fun generateDefaultLayoutParams(): RecyclerView.LayoutParams {
return RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT)
return RecyclerView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
}

private fun setPopupClipToScreenEnabled(clip: Boolean) {
Expand All @@ -294,8 +317,10 @@ class MaterialRecyclerViewPopupWindow(
private fun getMaxAvailableHeight(anchor: View, yOffset: Int, ignoreBottomDecorations: Boolean): Int {
sGetMaxAvailableHeightMethod?.let {
try {
return it.invoke(popup, anchor, yOffset,
ignoreBottomDecorations) as Int
return it.invoke(
popup, anchor, yOffset,
ignoreBottomDecorations
) as Int
} catch (e: Exception) {
Log.i(TAG, "Could not call getMaxAvailableHeightMethod(View, int, boolean)" + " on PopupWindow. Using the public version.")
}
Expand Down Expand Up @@ -334,5 +359,4 @@ class MaterialRecyclerViewPopupWindow(

return menuWidth
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package com.github.zawadz88.materialpopupmenu

import android.content.Context
import android.graphics.drawable.Drawable
import androidx.appcompat.widget.MaterialRecyclerViewPopupWindow
import android.view.View
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import androidx.annotation.LayoutRes
import androidx.annotation.StyleRes
import androidx.annotation.UiThread
import androidx.appcompat.widget.MaterialRecyclerViewPopupWindow
import com.github.zawadz88.materialpopupmenu.internal.PopupMenuAdapter


/**
* Holds all the required information for showing a popup menu.
*
Expand All @@ -22,12 +21,15 @@ import com.github.zawadz88.materialpopupmenu.internal.PopupMenuAdapter
* @author Piotr Zawadzki
*/
class MaterialPopupMenu internal constructor(
@StyleRes internal val style: Int,
internal val dropdownGravity: Int,
internal val sections: List<PopupMenuSection>) {
@StyleRes internal val style: Int,
internal val dropdownGravity: Int,
internal val sections: List<PopupMenuSection>
) {

private var popupWindow: MaterialRecyclerViewPopupWindow? = null

private var dismissListener: (() -> Unit)? = null

/**
* Shows a popup menu in the UI.
*
Expand All @@ -45,6 +47,7 @@ class MaterialPopupMenu internal constructor(

popupWindow.show()
this.popupWindow = popupWindow
setOnDismissListener(this.dismissListener)
}

/**
Expand All @@ -55,29 +58,37 @@ class MaterialPopupMenu internal constructor(
this.popupWindow?.dismiss()
}

/**
* Sets a listener that is called when this popup window is dismissed.
*
* @param listener Listener that is called when this popup window is dismissed.
*/
fun setOnDismissListener(listener: (() -> Unit)?) {
this.dismissListener = listener
this.popupWindow?.setOnDismissListener(listener)
}

internal data class PopupMenuSection(
val title: String?,
val items: List<AbstractPopupMenuItem>
val title: String?,
val items: List<AbstractPopupMenuItem>
)

internal data class PopupMenuItem(
val label: String,
@ColorInt val labelColor: Int,
@DrawableRes val icon: Int,
val iconDrawable: Drawable?,
@ColorInt val iconColor: Int,
override val callback: () -> Unit
val label: String,
@ColorInt val labelColor: Int,
@DrawableRes val icon: Int,
val iconDrawable: Drawable?,
@ColorInt val iconColor: Int,
override val callback: () -> Unit
) : AbstractPopupMenuItem(callback)

internal data class PopupMenuCustomItem(
@LayoutRes val layoutResId: Int,
val viewBoundCallback: (View) -> Unit,
override val callback: () -> Unit
@LayoutRes val layoutResId: Int,
val viewBoundCallback: (View) -> Unit,
override val callback: () -> Unit
) : AbstractPopupMenuItem(callback)


internal abstract class AbstractPopupMenuItem(
open val callback: () -> Unit
open val callback: () -> Unit
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import androidx.core.content.ContextCompat
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SwitchCompat
Expand All @@ -23,6 +24,10 @@ import com.github.zawadz88.materialpopupmenu.popupMenuBuilder
@SuppressLint("PrivateResource")
class DarkActivity : AppCompatActivity() {

companion object {
private const val TAG = "MPM"
}

@BindView(R.id.toolbar)
lateinit var toolbar: Toolbar

Expand Down Expand Up @@ -107,6 +112,9 @@ class DarkActivity : AppCompatActivity() {
}
}

popupMenu.setOnDismissListener {
Log.i(TAG, "Popup dismissed!")
}
popupMenu.show(this@DarkActivity, view)
}

Expand Down Expand Up @@ -134,6 +142,9 @@ class DarkActivity : AppCompatActivity() {
}

popupMenu.show(this@DarkActivity, view)
popupMenu.setOnDismissListener {
Log.i(TAG, "Popup dismissed!")
}
}

@OnClick(R.id.sectionsWithTitlesTextView)
Expand Down
Loading

0 comments on commit e02c549

Please sign in to comment.