Skip to content

Commit

Permalink
Release/4.0.0 (#64)
Browse files Browse the repository at this point in the history
* Moved `super.bindItem` call to the end of `ItemViewHolder#bindItem`method so that it's possible to override values set by it in callbacks.
Updated `MaterialPopupMenuBuilder.SectionHolder.label` and `MaterialPopupMenuBuilder.ItemHolder.label` to be of type `CharSequence` rather than `String` to allow the use of `Spannables`.
* Added an option to set a fixed width of the popup menu.
This can be used e.g. so that the popup menu has the same width as the anchor view. There was a sample added for this use case.
* Updates minimum supported SDK version to API 19.
Added an option to set a fixed width of the popup menu.
This can be used e.g. so that the popup menu has the same width as the anchor view. There was a sample added for this use case.
To achieve this effect an option to override offset was also added.
* Replaced xxLeft and xxRight attributes with xxStart and xxEnd RTL equivalents.
  • Loading branch information
zawadz88 authored Jul 6, 2019
1 parent ab65b75 commit 0b21ef7
Show file tree
Hide file tree
Showing 36 changed files with 485 additions and 150 deletions.
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [4.0.0]
### Changed
- *Breaking change:* minimum SDK version raised to API 19 (Kitkat)
- *Breaking change:* replaced `mpm_paddingLeft` with `mpm_paddingStart` and `mpm_paddingRight` with `mpm_paddingEnd` and other inter layout attributes accordingly for better RTL support
- *Breaking change:* updated `MaterialPopupMenuBuilder.SectionHolder.label` and `MaterialPopupMenuBuilder.ItemHolder.label` to be of type `CharSequence` rather than `String` to allow the use of `Spannables`.
- Moved `super.bindItem` call to the end of `ItemViewHolder#bindItem`method so that it's possible to override values set by it in callbacks.

### Added
- an option to override the defaults and set menu width & dropdown offsets programmatically via `MaterialPopupMenuBuilder`

## [3.4.0]
### Added
- An option to display an icon at the end of each item which indicates a nested submenu.
Expand Down Expand Up @@ -98,7 +108,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Added
Initial release of the library.

[Unreleased]: https://github.com/zawadz88/MaterialPopupMenu/compare/3.4.0...HEAD
[Unreleased]: https://github.com/zawadz88/MaterialPopupMenu/compare/4.0.0...HEAD
[4.0.0]: https://github.com/zawadz88/MaterialPopupMenu/compare/3.4.0...4.0.0
[3.4.0]: https://github.com/zawadz88/MaterialPopupMenu/compare/3.3.0...3.4.0
[3.3.0]: https://github.com/zawadz88/MaterialPopupMenu/compare/3.2.0...3.3.0
[3.2.0]: https://github.com/zawadz88/MaterialPopupMenu/compare/3.1.0...3.2.0
Expand Down
9 changes: 4 additions & 5 deletions 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.4.0'
implementation 'com.github.zawadz88.materialpopupmenu:material-popup-menu:4.0.0'
```

## Getting started
Expand Down Expand Up @@ -91,7 +91,7 @@ To create a popup menu with 2 sections and a section title in the second one:
* Adding additional offsets to where the dialog should be shown
* Keeping popup open after clicking on item
* Dimming background behind popup
* Customizing popup padding
* Customizing popup width, padding & offsets
* Displaying an icon at the end of each item which indicates a nested submenu

## Custom views
Expand Down Expand Up @@ -202,6 +202,5 @@ Check out the [official documentation](https://kotlinlang.org/docs/reference/bas
### I have not migrated to AndroidX. Can I use this library?
If you're still using legacy Android Support libraries you can use version 2.2.0. AndroidX is supported by default since 3.0.0.

## TODOs:
* add Espresso tests
* add item selection
### Can I use this library on Jelly Bean?
Up to 3.4.0 minimum supported version was API 16 (Jelly Bean) so you can use that version. Since 4.0.0 minimum supported version is API 19 (Kitkat).
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ allprojects {

configure(allprojects) {
ext {
ANDROID_MIN_SDK_VERSION = 16
ANDROID_MIN_SDK_VERSION = 19
ANDROID_TARGET_SDK_VERSION = 28
ANDROID_COMPILE_SDK_VERSION = 28
ANDROID_BUILD_TOOLS_VERSION = '28.0.3'
Expand Down
7 changes: 0 additions & 7 deletions docs/material-popup-menu/alltypes/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ <h3>All Types</h3>
</tr>
<tr>
<td>
<a href="../androidx.appcompat.widget/-material-recycler-view-popup-window/index.html">androidx.appcompat.widget.MaterialRecyclerViewPopupWindow</a></td>
<td>
<p>A more Material version of <a href="#">ListPopupWindow</a> based on <a href="#">RecyclerView</a>.</p>
</td>
</tr>
<tr>
<td>
<a href="../com.github.zawadz88.materialpopupmenu.internal/-sectioned-recycler-view-adapter/index.html">com.github.zawadz88.materialpopupmenu.internal.SectionedRecyclerViewAdapter</a></td>
<td>
<p>An extension to RecyclerView.Adapter to provide sections with headers to a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ <h3>Properties</h3>
<p><a href="label.html">label</a></p>
</td>
<td>
<code><span class="keyword">var </span><span class="identifier">label</span><span class="symbol">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html"><span class="identifier">String</span></a><span class="symbol">?</span></code>
<code><span class="keyword">var </span><span class="identifier">label</span><span class="symbol">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char-sequence/index.html"><span class="identifier">CharSequence</span></a><span class="symbol">?</span></code>
<p>Item label.</p>
</td>
</tr>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<br/>
<h1>label</h1>
<a name="com.github.zawadz88.materialpopupmenu.MaterialPopupMenuBuilder.ItemHolder$label"></a>
<code><span class="keyword">var </span><span class="identifier">label</span><span class="symbol">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html"><span class="identifier">String</span></a><span class="symbol">?</span></code>
<code><span class="keyword">var </span><span class="identifier">label</span><span class="symbol">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char-sequence/index.html"><span class="identifier">CharSequence</span></a><span class="symbol">?</span></code>
<p>Item label.</p>
<p>This is a required field and must not be <em>null</em>, unless you specify a label
using <a href="label-res.html">labelRes</a>.</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ <h3>Properties</h3>
<p><a href="title.html">title</a></p>
</td>
<td>
<code><span class="keyword">var </span><span class="identifier">title</span><span class="symbol">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html"><span class="identifier">String</span></a><span class="symbol">?</span></code>
<code><span class="keyword">var </span><span class="identifier">title</span><span class="symbol">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char-sequence/index.html"><span class="identifier">CharSequence</span></a><span class="symbol">?</span></code>
<p>Optional section holder. <em>null</em> by default.
If the title is not <em>null</em> it will be displayed in the menu.</p>
</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<br/>
<h1>title</h1>
<a name="com.github.zawadz88.materialpopupmenu.MaterialPopupMenuBuilder.SectionHolder$title"></a>
<code><span class="keyword">var </span><span class="identifier">title</span><span class="symbol">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html"><span class="identifier">String</span></a><span class="symbol">?</span></code>
<code><span class="keyword">var </span><span class="identifier">title</span><span class="symbol">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char-sequence/index.html"><span class="identifier">CharSequence</span></a><span class="symbol">?</span></code>
<p>Optional section holder. <em>null</em> by default.
If the title is not <em>null</em> it will be displayed in the menu.</p>
</BODY>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<HTML>
<HEAD>
<meta charset="UTF-8">
<title>MaterialPopupMenuBuilder.dropDownHorizontalOffset - 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">MaterialPopupMenuBuilder</a>&nbsp;/&nbsp;<a href="./drop-down-horizontal-offset.html">dropDownHorizontalOffset</a><br/>
<br/>
<h1>dropDownHorizontalOffset</h1>
<a name="com.github.zawadz88.materialpopupmenu.MaterialPopupMenuBuilder$dropDownHorizontalOffset"></a>
<code><span class="keyword">var </span><span class="identifier">dropDownHorizontalOffset</span><span class="symbol">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html"><span class="identifier">Int</span></a><span class="symbol">?</span></code>
<p>Setting this to non-<code>null</code> value will override <code>android:dropDownHorizontalOffset</code> set by the style applied in <a href="style.html">style</a>.</p>
</BODY>
</HTML>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<HTML>
<HEAD>
<meta charset="UTF-8">
<title>MaterialPopupMenuBuilder.dropDownVerticalOffset - 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">MaterialPopupMenuBuilder</a>&nbsp;/&nbsp;<a href="./drop-down-vertical-offset.html">dropDownVerticalOffset</a><br/>
<br/>
<h1>dropDownVerticalOffset</h1>
<a name="com.github.zawadz88.materialpopupmenu.MaterialPopupMenuBuilder$dropDownVerticalOffset"></a>
<code><span class="keyword">var </span><span class="identifier">dropDownVerticalOffset</span><span class="symbol">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html"><span class="identifier">Int</span></a><span class="symbol">?</span></code>
<p>Setting this to non-<code>null</code> value will override <code>android:dropDownVerticalOffset</code> set by the style applied in <a href="style.html">style</a>.</p>
</BODY>
</HTML>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<HTML>
<HEAD>
<meta charset="UTF-8">
<title>MaterialPopupMenuBuilder.fixedContentWidthInPx - 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">MaterialPopupMenuBuilder</a>&nbsp;/&nbsp;<a href="./fixed-content-width-in-px.html">fixedContentWidthInPx</a><br/>
<br/>
<h1>fixedContentWidthInPx</h1>
<a name="com.github.zawadz88.materialpopupmenu.MaterialPopupMenuBuilder$fixedContentWidthInPx"></a>
<code><span class="keyword">var </span><span class="identifier">fixedContentWidthInPx</span><span class="symbol">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html"><span class="identifier">Int</span></a></code>
<p>Setting this to a non-zero value will force the width of the popup menu to be exactly this value.
If set to 0, the default mechanism for measuring popup menu width will be applied.</p>
</BODY>
</HTML>
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,34 @@ <h3>Properties</h3>
</tr>
<tr>
<td>
<p><a href="drop-down-horizontal-offset.html">dropDownHorizontalOffset</a></p>
</td>
<td>
<code><span class="keyword">var </span><span class="identifier">dropDownHorizontalOffset</span><span class="symbol">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html"><span class="identifier">Int</span></a><span class="symbol">?</span></code>
<p>Setting this to non-<code>null</code> value will override <code>android:dropDownHorizontalOffset</code> set by the style applied in <a href="style.html">style</a>.</p>
</td>
</tr>
<tr>
<td>
<p><a href="drop-down-vertical-offset.html">dropDownVerticalOffset</a></p>
</td>
<td>
<code><span class="keyword">var </span><span class="identifier">dropDownVerticalOffset</span><span class="symbol">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html"><span class="identifier">Int</span></a><span class="symbol">?</span></code>
<p>Setting this to non-<code>null</code> value will override <code>android:dropDownVerticalOffset</code> set by the style applied in <a href="style.html">style</a>.</p>
</td>
</tr>
<tr>
<td>
<p><a href="fixed-content-width-in-px.html">fixedContentWidthInPx</a></p>
</td>
<td>
<code><span class="keyword">var </span><span class="identifier">fixedContentWidthInPx</span><span class="symbol">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html"><span class="identifier">Int</span></a></code>
<p>Setting this to a non-zero value will force the width of the popup menu to be exactly this value.
If set to 0, the default mechanism for measuring popup menu width will be applied.</p>
</td>
</tr>
<tr>
<td>
<p><a href="style.html">style</a></p>
</td>
<td>
Expand Down
7 changes: 0 additions & 7 deletions docs/material-popup-menu/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ <h3>Packages</h3>
<tbody>
<tr>
<td>
<p><a href="androidx.appcompat.widget/index.html">androidx.appcompat.widget</a></p>
</td>
<td>
</td>
</tr>
<tr>
<td>
<p><a href="com.github.zawadz88.materialpopupmenu/index.html">com.github.zawadz88.materialpopupmenu</a></p>
</td>
<td>
Expand Down
1 change: 0 additions & 1 deletion docs/material-popup-menu/package-list
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
$dokka.format:html
$dokka.linkExtension:html

androidx.appcompat.widget
com.github.zawadz88.materialpopupmenu
com.github.zawadz88.materialpopupmenu.internal
2 changes: 1 addition & 1 deletion material-popup-menu/material-popup-menu.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ ext {
siteUrl = 'https://github.com/zawadz88/MaterialPopupMenu'
gitUrl = 'https://github.com/zawadz88/MaterialPopupMenu.git'

libraryVersion = '3.4.0'
libraryVersion = '4.0.0'

developerId = 'zawadz88'
developerName = 'Piotr Zawadzki'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ import java.lang.reflect.Method
* @see ListPopupWindow
*/
@SuppressLint("PrivateResource,RestrictedApi")
class MaterialRecyclerViewPopupWindow(
internal class MaterialRecyclerViewPopupWindow(
private val context: Context,
private var dropDownGravity: Int
private var dropDownGravity: Int,
private val fixedContentWidthInPx: Int,
dropDownVerticalOffset: Int?,
dropDownHorizontalOffset: Int?
) {

companion object {
Expand Down Expand Up @@ -66,7 +69,10 @@ class MaterialRecyclerViewPopupWindow(

internal var adapter: PopupMenuAdapter? = null
set(value) {
setContentWidth(measureIndividualMenuWidth(checkNotNull(value)))
val menuWidth = measureMenuSizeAndGetWidth(checkNotNull(value))
if (fixedContentWidthInPx == 0) {
updateContentWidth(menuWidth)
}
field = value
}

Expand Down Expand Up @@ -96,9 +102,9 @@ class MaterialRecyclerViewPopupWindow(

private val popupPaddingBottom: Int

private val popupPaddingLeft: Int
private val popupPaddingStart: Int

private val popupPaddingRight: Int
private val popupPaddingEnd: Int

private val popupPaddingTop: Int

Expand All @@ -113,16 +119,20 @@ class MaterialRecyclerViewPopupWindow(

val a = context.obtainStyledAttributes(null, R.styleable.MaterialRecyclerViewPopupWindow)

dropDownHorizontalOffset = a.getDimensionPixelOffset(R.styleable.MaterialRecyclerViewPopupWindow_android_dropDownHorizontalOffset, 0)
dropDownVerticalOffset = a.getDimensionPixelOffset(R.styleable.MaterialRecyclerViewPopupWindow_android_dropDownVerticalOffset, 0)
this.dropDownHorizontalOffset = dropDownHorizontalOffset ?: a.getDimensionPixelOffset(R.styleable.MaterialRecyclerViewPopupWindow_android_dropDownHorizontalOffset, 0)
this.dropDownVerticalOffset = dropDownVerticalOffset ?: a.getDimensionPixelOffset(R.styleable.MaterialRecyclerViewPopupWindow_android_dropDownVerticalOffset, 0)
backgroundDimEnabled = a.getBoolean(R.styleable.MaterialRecyclerViewPopupWindow_android_backgroundDimEnabled, false)
backgroundDimAmount = a.getFloat(R.styleable.MaterialRecyclerViewPopupWindow_android_backgroundDimAmount, DEFAULT_BACKGROUND_DIM_AMOUNT)
popupPaddingBottom = a.getDimensionPixelSize(R.styleable.MaterialRecyclerViewPopupWindow_mpm_paddingBottom, 0)
popupPaddingLeft = a.getDimensionPixelSize(R.styleable.MaterialRecyclerViewPopupWindow_mpm_paddingLeft, 0)
popupPaddingRight = a.getDimensionPixelSize(R.styleable.MaterialRecyclerViewPopupWindow_mpm_paddingRight, 0)
popupPaddingStart = a.getDimensionPixelSize(R.styleable.MaterialRecyclerViewPopupWindow_mpm_paddingStart, 0)
popupPaddingEnd = a.getDimensionPixelSize(R.styleable.MaterialRecyclerViewPopupWindow_mpm_paddingEnd, 0)
popupPaddingTop = a.getDimensionPixelSize(R.styleable.MaterialRecyclerViewPopupWindow_mpm_paddingTop, 0)

a.recycle()

if (fixedContentWidthInPx != 0) {
updateContentWidth(fixedContentWidthInPx)
}
}

/**
Expand All @@ -131,7 +141,7 @@ class MaterialRecyclerViewPopupWindow(
* @param width Desired width of content in pixels.
*/
private fun setContentWidth(width: Int) {
private fun updateContentWidth(width: Int) {
val popupBackground = popup.background
dropDownWidth = if (popupBackground != null) {
popupBackground.getPadding(tempRect)
Expand All @@ -145,7 +155,7 @@ class MaterialRecyclerViewPopupWindow(
* Show the popupMenu list. If the list is already showing, this method
* will recalculate the popupMenu's size and position.
*/
fun show() {
internal fun show() {
checkNotNull(anchorView) { "Anchor view must be set!" }
val height = buildDropDown()

Expand Down Expand Up @@ -183,7 +193,7 @@ class MaterialRecyclerViewPopupWindow(
/**
* Dismiss the popupMenu window.
*/
fun dismiss() {
internal fun dismiss() {
popup.dismiss()
popup.contentView = null
}
Expand All @@ -193,7 +203,7 @@ class MaterialRecyclerViewPopupWindow(
*
* @param listener Listener that is called when this popup window is dismissed.
*/
fun setOnDismissListener(listener: (() -> Unit)?) {
internal fun setOnDismissListener(listener: (() -> Unit)?) {
if (listener != null) {
popup.setOnDismissListener { listener.invoke() }
} else {
Expand All @@ -212,16 +222,20 @@ class MaterialRecyclerViewPopupWindow(
var otherHeights = 0

val dropDownList = View.inflate(context, R.layout.mpm_popup_menu, null) as RecyclerView
dropDownList.adapter = adapter
dropDownList.layoutManager = LinearLayoutManager(context)
dropDownList.isFocusable = true
dropDownList.isFocusableInTouchMode = true
dropDownList.setPadding(popupPaddingLeft, popupPaddingTop, popupPaddingRight, popupPaddingBottom)
dropDownList.also {
it.adapter = adapter
it.layoutManager = LinearLayoutManager(context)
it.isFocusable = true
it.isFocusableInTouchMode = true
it.setPaddingRelative(popupPaddingStart, popupPaddingTop, popupPaddingEnd, popupPaddingBottom)
}

val background = popup.background

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
dropDownList.clipToOutline = true
// Move the background from popup to RecyclerView for clipToOutline to take effect.
dropDownList.background = popup.background
dropDownList.background = background
popup.setBackgroundDrawable(null)
}

Expand All @@ -230,7 +244,6 @@ class MaterialRecyclerViewPopupWindow(
// getMaxAvailableHeight() subtracts the padding, so we put it back
// to get the available height for the whole window.
val padding: Int
val background = popup.background
if (background != null) {
background.getPadding(tempRect)
padding = tempRect.top + tempRect.bottom
Expand Down Expand Up @@ -367,7 +380,7 @@ class MaterialRecyclerViewPopupWindow(
/**
* @see android.support.v7.view.menu.MenuPopup.measureIndividualMenuWidth
*/
private fun measureIndividualMenuWidth(adapter: PopupMenuAdapter): Int {
private fun measureMenuSizeAndGetWidth(adapter: PopupMenuAdapter): Int {
adapter.setupIndices()
val parent = FrameLayout(context)
var menuWidth = popupMinWidth
Expand Down
Loading

0 comments on commit 0b21ef7

Please sign in to comment.