Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. package com.vuzz.snapdish.ui;
  2.  
  3. import android.content.Context;
  4.  
  5. import android.support.v7.view.SupportMenuInflater;
  6. import android.support.v7.view.menu.MenuBuilder;
  7. import android.support.v7.view.menu.MenuPopupHelper;
  8. import android.support.v7.view.menu.MenuPresenter;
  9. import android.support.v7.view.menu.SubMenuBuilder;
  10. import android.view.Menu;
  11. import android.view.MenuInflater;
  12. import android.view.MenuItem;
  13. import android.view.View;
  14.  
  15. /**
  16. * Copied from android.support.v7.widget.PopupMenu.
  17. * "mPopup.setForceShowIcon(true);" in the constructor does the trick :)
  18. *
  19. * @author maikvlcek
  20. * @since 5:00 PM - 1/27/14
  21. */
  22. public class IconizedMenu implements MenuBuilder.Callback, MenuPresenter.Callback {
  23. private Context mContext;
  24. private MenuBuilder mMenu;
  25. private View mAnchor;
  26. private MenuPopupHelper mPopup;
  27. private OnMenuItemClickListener mMenuItemClickListener;
  28. private OnDismissListener mDismissListener;
  29.  
  30. /**
  31. * Callback interface used to notify the application that the menu has closed.
  32. */
  33. public interface OnDismissListener {
  34. /**
  35. * Called when the associated menu has been dismissed.
  36. *
  37. * @param menu The PopupMenu that was dismissed.
  38. */
  39. public void onDismiss(IconizedMenu menu);
  40. }
  41.  
  42. /**
  43. * Construct a new PopupMenu.
  44. *
  45. * @param context Context for the PopupMenu.
  46. * @param anchor Anchor view for this popup. The popup will appear below the anchor if there
  47. * is room, or above it if there is not.
  48. */
  49. public IconizedMenu(Context context, View anchor) {
  50. mContext = context;
  51. mMenu = new MenuBuilder(context);
  52. mMenu.setCallback(this);
  53. mAnchor = anchor;
  54. mPopup = new MenuPopupHelper(context, mMenu, anchor);
  55. mPopup.setPresenterCallback(this);
  56. mPopup.setForceShowIcon(true);
  57. }
  58.  
  59. /**
  60. * @return the {@link android.view.Menu} associated with this popup. Populate the returned Menu with
  61. * items before calling {@link #show()}.
  62. *
  63. * @see #show()
  64. * @see #getMenuInflater()
  65. */
  66. public Menu getMenu() {
  67. return mMenu;
  68. }
  69.  
  70. /**
  71. * @return a {@link android.view.MenuInflater} that can be used to inflate menu items from XML into the
  72. * menu returned by {@link #getMenu()}.
  73. *
  74. * @see #getMenu()
  75. */
  76. public MenuInflater getMenuInflater() {
  77. return new SupportMenuInflater(mContext);
  78. }
  79.  
  80. /**
  81. * Inflate a menu resource into this PopupMenu. This is equivalent to calling
  82. * popupMenu.getMenuInflater().inflate(menuRes, popupMenu.getMenu()).
  83. * @param menuRes Menu resource to inflate
  84. */
  85. public void inflate(int menuRes) {
  86. getMenuInflater().inflate(menuRes, mMenu);
  87. }
  88.  
  89. /**
  90. * Show the menu popup anchored to the view specified during construction.
  91. * @see #dismiss()
  92. */
  93. public void show() {
  94. mPopup.show();
  95. }
  96.  
  97. /**
  98. * Dismiss the menu popup.
  99. * @see #show()
  100. */
  101. public void dismiss() {
  102. mPopup.dismiss();
  103. }
  104.  
  105. /**
  106. * Set a listener that will be notified when the user selects an item from the menu.
  107. *
  108. * @param listener Listener to notify
  109. */
  110. public void setOnMenuItemClickListener(OnMenuItemClickListener listener) {
  111. mMenuItemClickListener = listener;
  112. }
  113.  
  114. /**
  115. * Set a listener that will be notified when this menu is dismissed.
  116. *
  117. * @param listener Listener to notify
  118. */
  119. public void setOnDismissListener(OnDismissListener listener) {
  120. mDismissListener = listener;
  121. }
  122.  
  123. /**
  124. * @hide
  125. */
  126. public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
  127. if (mMenuItemClickListener != null) {
  128. return mMenuItemClickListener.onMenuItemClick(item);
  129. }
  130. return false;
  131. }
  132.  
  133. /**
  134. * @hide
  135. */
  136. public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
  137. if (mDismissListener != null) {
  138. mDismissListener.onDismiss(this);
  139. }
  140. }
  141.  
  142. /**
  143. * @hide
  144. */
  145. public boolean onOpenSubMenu(MenuBuilder subMenu) {
  146. if (subMenu == null) return false;
  147.  
  148. if (!subMenu.hasVisibleItems()) {
  149. return true;
  150. }
  151.  
  152. // Current menu will be dismissed by the normal helper, submenu will be shown in its place.
  153. new MenuPopupHelper(mContext, subMenu, mAnchor).show();
  154. return true;
  155. }
  156.  
  157. /**
  158. * @hide
  159. */
  160. public void onCloseSubMenu(SubMenuBuilder menu) {
  161. }
  162.  
  163. /**
  164. * @hide
  165. */
  166. public void onMenuModeChange(MenuBuilder menu) {
  167. }
  168.  
  169. /**
  170. * Interface responsible for receiving menu item click events if the items themselves
  171. * do not have individual item click listeners.
  172. */
  173. public interface OnMenuItemClickListener {
  174. /**
  175. * This method will be invoked when a menu item is clicked if the item itself did
  176. * not already handle the event.
  177. *
  178. * @param item {@link MenuItem} that was clicked
  179. * @return <code>true</code> if the event was handled, <code>false</code> otherwise.
  180. */
  181. public boolean onMenuItemClick(MenuItem item);
  182. }
  183.  
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement