Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.internal.view.menu; |
| 18 | |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 19 | import android.content.Context; |
Adam Powell | 38639b1 | 2011-06-17 16:02:32 -0700 | [diff] [blame] | 20 | import android.content.res.Resources; |
Adam Powell | 11ed1d6 | 2011-07-11 21:19:59 -0700 | [diff] [blame] | 21 | import android.os.Parcelable; |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 22 | import android.view.KeyEvent; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 23 | import android.view.LayoutInflater; |
Adam Powell | 9151103 | 2011-07-13 10:24:06 -0700 | [diff] [blame] | 24 | import android.view.MenuItem; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 25 | import android.view.View; |
| 26 | import android.view.View.MeasureSpec; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 27 | import android.view.ViewGroup; |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 28 | import android.view.ViewTreeObserver; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 29 | import android.widget.AdapterView; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 30 | import android.widget.BaseAdapter; |
Adam Powell | 9151103 | 2011-07-13 10:24:06 -0700 | [diff] [blame] | 31 | import android.widget.FrameLayout; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 32 | import android.widget.ListAdapter; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 33 | import android.widget.ListPopupWindow; |
Adam Powell | 6c6f575 | 2010-08-20 18:34:46 -0700 | [diff] [blame] | 34 | import android.widget.PopupWindow; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 35 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 36 | import java.util.ArrayList; |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 37 | |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 38 | /** |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 39 | * Presents a menu as a small, simple popup anchored to another view. |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 40 | * @hide |
| 41 | */ |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 42 | public class MenuPopupHelper implements AdapterView.OnItemClickListener, View.OnKeyListener, |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 43 | ViewTreeObserver.OnGlobalLayoutListener, PopupWindow.OnDismissListener, |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 44 | View.OnAttachStateChangeListener, MenuPresenter { |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 45 | private static final String TAG = "MenuPopupHelper"; |
| 46 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 47 | static final int ITEM_LAYOUT = com.android.internal.R.layout.popup_menu_item_layout; |
| 48 | |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 49 | private final Context mContext; |
| 50 | private final LayoutInflater mInflater; |
| 51 | private final MenuBuilder mMenu; |
| 52 | private final MenuAdapter mAdapter; |
| 53 | private final boolean mOverflowOnly; |
| 54 | private final int mPopupMaxWidth; |
| 55 | |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 56 | private View mAnchorView; |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 57 | private ListPopupWindow mPopup; |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 58 | private ViewTreeObserver mTreeObserver; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 59 | private Callback mPresenterCallback; |
Adam Powell | e2b03a6 | 2011-01-25 14:39:02 -0800 | [diff] [blame] | 60 | |
Adam Powell | 9151103 | 2011-07-13 10:24:06 -0700 | [diff] [blame] | 61 | boolean mForceShowIcon; |
| 62 | |
| 63 | private ViewGroup mMeasureParent; |
| 64 | |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 65 | /** Whether the cached content width value is valid. */ |
| 66 | private boolean mHasContentWidth; |
| 67 | |
| 68 | /** Cached content width from {@link #measureContentWidth}. */ |
| 69 | private int mContentWidth; |
| 70 | |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 71 | public MenuPopupHelper(Context context, MenuBuilder menu) { |
| 72 | this(context, menu, null, false); |
| 73 | } |
| 74 | |
| 75 | public MenuPopupHelper(Context context, MenuBuilder menu, View anchorView) { |
| 76 | this(context, menu, anchorView, false); |
| 77 | } |
| 78 | |
| 79 | public MenuPopupHelper(Context context, MenuBuilder menu, |
| 80 | View anchorView, boolean overflowOnly) { |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 81 | mContext = context; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 82 | mInflater = LayoutInflater.from(context); |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 83 | mMenu = menu; |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 84 | mAdapter = new MenuAdapter(mMenu); |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 85 | mOverflowOnly = overflowOnly; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 86 | |
Adam Powell | 38639b1 | 2011-06-17 16:02:32 -0700 | [diff] [blame] | 87 | final Resources res = context.getResources(); |
| 88 | mPopupMaxWidth = Math.max(res.getDisplayMetrics().widthPixels / 2, |
| 89 | res.getDimensionPixelSize(com.android.internal.R.dimen.config_prefDialogWidth)); |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 90 | |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 91 | mAnchorView = anchorView; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 92 | |
| 93 | menu.addMenuPresenter(this); |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Adam Powell | f0ad6e6 | 2011-01-10 17:14:06 -0800 | [diff] [blame] | 96 | public void setAnchorView(View anchor) { |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 97 | mAnchorView = anchor; |
Adam Powell | f0ad6e6 | 2011-01-10 17:14:06 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Adam Powell | 9151103 | 2011-07-13 10:24:06 -0700 | [diff] [blame] | 100 | public void setForceShowIcon(boolean forceShow) { |
| 101 | mForceShowIcon = forceShow; |
| 102 | } |
| 103 | |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 104 | public void show() { |
Adam Powell | 5e3f284 | 2011-01-07 17:16:56 -0800 | [diff] [blame] | 105 | if (!tryShow()) { |
| 106 | throw new IllegalStateException("MenuPopupHelper cannot be used without an anchor"); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | public boolean tryShow() { |
Adam Powell | 0b2d306 | 2010-09-14 16:15:02 -0700 | [diff] [blame] | 111 | mPopup = new ListPopupWindow(mContext, null, com.android.internal.R.attr.popupMenuStyle); |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 112 | mPopup.setOnDismissListener(this); |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 113 | mPopup.setOnItemClickListener(this); |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 114 | mPopup.setAdapter(mAdapter); |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 115 | mPopup.setModal(true); |
| 116 | |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 117 | View anchor = mAnchorView; |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 118 | if (anchor != null) { |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 119 | final boolean addGlobalListener = mTreeObserver == null; |
| 120 | mTreeObserver = anchor.getViewTreeObserver(); // Refresh to latest |
| 121 | if (addGlobalListener) mTreeObserver.addOnGlobalLayoutListener(this); |
| 122 | anchor.addOnAttachStateChangeListener(this); |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 123 | mPopup.setAnchorView(anchor); |
Adam Powell | 4be0d52 | 2010-08-03 17:53:14 -0700 | [diff] [blame] | 124 | } else { |
Adam Powell | 5e3f284 | 2011-01-07 17:16:56 -0800 | [diff] [blame] | 125 | return false; |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 126 | } |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 127 | |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 128 | if (!mHasContentWidth) { |
| 129 | mContentWidth = measureContentWidth(); |
| 130 | mHasContentWidth = true; |
| 131 | } |
| 132 | |
| 133 | mPopup.setContentWidth(mContentWidth); |
Adam Powell | aa0b92c | 2010-12-13 22:38:53 -0800 | [diff] [blame] | 134 | mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED); |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 135 | mPopup.show(); |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 136 | mPopup.getListView().setOnKeyListener(this); |
Adam Powell | 5e3f284 | 2011-01-07 17:16:56 -0800 | [diff] [blame] | 137 | return true; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | public void dismiss() { |
Adam Powell | 3d3da27 | 2010-08-11 18:06:17 -0700 | [diff] [blame] | 141 | if (isShowing()) { |
| 142 | mPopup.dismiss(); |
| 143 | } |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | public void onDismiss() { |
| 147 | mPopup = null; |
Adam Powell | 42b91bb | 2011-06-21 18:32:26 -0700 | [diff] [blame] | 148 | mMenu.close(); |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 149 | if (mTreeObserver != null) { |
| 150 | if (!mTreeObserver.isAlive()) mTreeObserver = mAnchorView.getViewTreeObserver(); |
Adam Powell | ca51e87 | 2011-02-14 19:54:29 -0800 | [diff] [blame] | 151 | mTreeObserver.removeGlobalOnLayoutListener(this); |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 152 | mTreeObserver = null; |
Adam Powell | ed8b403 | 2010-11-16 10:22:35 -0800 | [diff] [blame] | 153 | } |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 154 | mAnchorView.removeOnAttachStateChangeListener(this); |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 157 | public boolean isShowing() { |
| 158 | return mPopup != null && mPopup.isShowing(); |
| 159 | } |
| 160 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 161 | @Override |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 162 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 163 | MenuAdapter adapter = mAdapter; |
| 164 | adapter.mAdapterMenu.performItemAction(adapter.getItem(position), 0); |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 167 | public boolean onKey(View v, int keyCode, KeyEvent event) { |
| 168 | if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_MENU) { |
| 169 | dismiss(); |
| 170 | return true; |
| 171 | } |
| 172 | return false; |
| 173 | } |
| 174 | |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 175 | private int measureContentWidth() { |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 176 | // Menus don't tend to be long, so this is more sane than it looks. |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 177 | int maxWidth = 0; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 178 | View itemView = null; |
Adam Powell | 50f784c | 2010-12-19 16:12:19 -0800 | [diff] [blame] | 179 | int itemType = 0; |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 180 | |
| 181 | final ListAdapter adapter = mAdapter; |
| 182 | final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
| 183 | final int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 184 | final int count = adapter.getCount(); |
| 185 | for (int i = 0; i < count; i++) { |
Adam Powell | 50f784c | 2010-12-19 16:12:19 -0800 | [diff] [blame] | 186 | final int positionType = adapter.getItemViewType(i); |
| 187 | if (positionType != itemType) { |
| 188 | itemType = positionType; |
| 189 | itemView = null; |
| 190 | } |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 191 | |
Adam Powell | 9151103 | 2011-07-13 10:24:06 -0700 | [diff] [blame] | 192 | if (mMeasureParent == null) { |
| 193 | mMeasureParent = new FrameLayout(mContext); |
| 194 | } |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 195 | |
Adam Powell | 9151103 | 2011-07-13 10:24:06 -0700 | [diff] [blame] | 196 | itemView = adapter.getView(i, itemView, mMeasureParent); |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 197 | itemView.measure(widthMeasureSpec, heightMeasureSpec); |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 198 | |
| 199 | final int itemWidth = itemView.getMeasuredWidth(); |
| 200 | if (itemWidth >= mPopupMaxWidth) { |
| 201 | return mPopupMaxWidth; |
| 202 | } else if (itemWidth > maxWidth) { |
| 203 | maxWidth = itemWidth; |
| 204 | } |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 205 | } |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 206 | |
| 207 | return maxWidth; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 208 | } |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 209 | |
| 210 | @Override |
| 211 | public void onGlobalLayout() { |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 212 | if (isShowing()) { |
| 213 | final View anchor = mAnchorView; |
Adam Powell | ca51e87 | 2011-02-14 19:54:29 -0800 | [diff] [blame] | 214 | if (anchor == null || !anchor.isShown()) { |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 215 | dismiss(); |
Adam Powell | ca51e87 | 2011-02-14 19:54:29 -0800 | [diff] [blame] | 216 | } else if (isShowing()) { |
Adam Powell | aa0b92c | 2010-12-13 22:38:53 -0800 | [diff] [blame] | 217 | // Recompute window size and position |
| 218 | mPopup.show(); |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | } |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 222 | |
| 223 | @Override |
| 224 | public void onViewAttachedToWindow(View v) { |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public void onViewDetachedFromWindow(View v) { |
| 229 | if (mTreeObserver != null) { |
| 230 | if (!mTreeObserver.isAlive()) mTreeObserver = v.getViewTreeObserver(); |
| 231 | mTreeObserver.removeGlobalOnLayoutListener(this); |
| 232 | } |
| 233 | v.removeOnAttachStateChangeListener(this); |
| 234 | } |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 235 | |
| 236 | @Override |
| 237 | public void initForMenu(Context context, MenuBuilder menu) { |
| 238 | // Don't need to do anything; we added as a presenter in the constructor. |
| 239 | } |
| 240 | |
| 241 | @Override |
| 242 | public MenuView getMenuView(ViewGroup root) { |
| 243 | throw new UnsupportedOperationException("MenuPopupHelpers manage their own views"); |
| 244 | } |
| 245 | |
| 246 | @Override |
| 247 | public void updateMenuView(boolean cleared) { |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 248 | mHasContentWidth = false; |
| 249 | |
| 250 | if (mAdapter != null) { |
| 251 | mAdapter.notifyDataSetChanged(); |
| 252 | } |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | @Override |
| 256 | public void setCallback(Callback cb) { |
| 257 | mPresenterCallback = cb; |
| 258 | } |
| 259 | |
| 260 | @Override |
| 261 | public boolean onSubMenuSelected(SubMenuBuilder subMenu) { |
| 262 | if (subMenu.hasVisibleItems()) { |
| 263 | MenuPopupHelper subPopup = new MenuPopupHelper(mContext, subMenu, mAnchorView, false); |
| 264 | subPopup.setCallback(mPresenterCallback); |
Adam Powell | 9151103 | 2011-07-13 10:24:06 -0700 | [diff] [blame] | 265 | |
| 266 | boolean preserveIconSpacing = false; |
| 267 | final int count = subMenu.size(); |
| 268 | for (int i = 0; i < count; i++) { |
| 269 | MenuItem childItem = subMenu.getItem(i); |
| 270 | if (childItem.isVisible() && childItem.getIcon() != null) { |
| 271 | preserveIconSpacing = true; |
| 272 | break; |
| 273 | } |
| 274 | } |
| 275 | subPopup.setForceShowIcon(preserveIconSpacing); |
| 276 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 277 | if (subPopup.tryShow()) { |
| 278 | if (mPresenterCallback != null) { |
| 279 | mPresenterCallback.onOpenSubMenu(subMenu); |
| 280 | } |
| 281 | return true; |
| 282 | } |
| 283 | } |
| 284 | return false; |
| 285 | } |
| 286 | |
| 287 | @Override |
| 288 | public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) { |
| 289 | // Only care about the (sub)menu we're presenting. |
| 290 | if (menu != mMenu) return; |
| 291 | |
| 292 | dismiss(); |
| 293 | if (mPresenterCallback != null) { |
| 294 | mPresenterCallback.onCloseMenu(menu, allMenusAreClosing); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | @Override |
| 299 | public boolean flagActionItems() { |
| 300 | return false; |
| 301 | } |
| 302 | |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 303 | public boolean expandItemActionView(MenuBuilder menu, MenuItemImpl item) { |
| 304 | return false; |
| 305 | } |
| 306 | |
| 307 | public boolean collapseItemActionView(MenuBuilder menu, MenuItemImpl item) { |
| 308 | return false; |
| 309 | } |
| 310 | |
Adam Powell | 275702c | 2011-09-23 17:34:04 -0700 | [diff] [blame] | 311 | @Override |
| 312 | public int getId() { |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | @Override |
| 317 | public Parcelable onSaveInstanceState() { |
| 318 | return null; |
| 319 | } |
| 320 | |
| 321 | @Override |
| 322 | public void onRestoreInstanceState(Parcelable state) { |
| 323 | } |
| 324 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 325 | private class MenuAdapter extends BaseAdapter { |
| 326 | private MenuBuilder mAdapterMenu; |
Adam Powell | 275702c | 2011-09-23 17:34:04 -0700 | [diff] [blame] | 327 | private int mExpandedIndex = -1; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 328 | |
| 329 | public MenuAdapter(MenuBuilder menu) { |
| 330 | mAdapterMenu = menu; |
Adam Powell | 275702c | 2011-09-23 17:34:04 -0700 | [diff] [blame] | 331 | findExpandedIndex(); |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | public int getCount() { |
| 335 | ArrayList<MenuItemImpl> items = mOverflowOnly ? |
| 336 | mAdapterMenu.getNonActionItems() : mAdapterMenu.getVisibleItems(); |
Adam Powell | 275702c | 2011-09-23 17:34:04 -0700 | [diff] [blame] | 337 | if (mExpandedIndex < 0) { |
| 338 | return items.size(); |
| 339 | } |
| 340 | return items.size() - 1; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | public MenuItemImpl getItem(int position) { |
| 344 | ArrayList<MenuItemImpl> items = mOverflowOnly ? |
| 345 | mAdapterMenu.getNonActionItems() : mAdapterMenu.getVisibleItems(); |
Adam Powell | 275702c | 2011-09-23 17:34:04 -0700 | [diff] [blame] | 346 | if (mExpandedIndex >= 0 && position >= mExpandedIndex) { |
| 347 | position++; |
| 348 | } |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 349 | return items.get(position); |
| 350 | } |
| 351 | |
| 352 | public long getItemId(int position) { |
| 353 | // Since a menu item's ID is optional, we'll use the position as an |
| 354 | // ID for the item in the AdapterView |
| 355 | return position; |
| 356 | } |
| 357 | |
| 358 | public View getView(int position, View convertView, ViewGroup parent) { |
| 359 | if (convertView == null) { |
| 360 | convertView = mInflater.inflate(ITEM_LAYOUT, parent, false); |
| 361 | } |
| 362 | |
| 363 | MenuView.ItemView itemView = (MenuView.ItemView) convertView; |
Adam Powell | 9151103 | 2011-07-13 10:24:06 -0700 | [diff] [blame] | 364 | if (mForceShowIcon) { |
| 365 | ((ListMenuItemView) convertView).setForceShowIcon(true); |
| 366 | } |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 367 | itemView.initialize(getItem(position), 0); |
| 368 | return convertView; |
| 369 | } |
Adam Powell | 275702c | 2011-09-23 17:34:04 -0700 | [diff] [blame] | 370 | |
| 371 | void findExpandedIndex() { |
| 372 | final MenuItemImpl expandedItem = mMenu.getExpandedItem(); |
| 373 | if (expandedItem != null) { |
| 374 | final ArrayList<MenuItemImpl> items = mMenu.getNonActionItems(); |
| 375 | final int count = items.size(); |
| 376 | for (int i = 0; i < count; i++) { |
| 377 | final MenuItemImpl item = items.get(i); |
| 378 | if (item == expandedItem) { |
| 379 | mExpandedIndex = i; |
| 380 | return; |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | mExpandedIndex = -1; |
| 385 | } |
Adam Powell | 11ed1d6 | 2011-07-11 21:19:59 -0700 | [diff] [blame] | 386 | |
Adam Powell | 275702c | 2011-09-23 17:34:04 -0700 | [diff] [blame] | 387 | @Override |
Adam Powell | 76889f3 | 2012-05-08 22:22:52 -0700 | [diff] [blame] | 388 | public void notifyDataSetChanged() { |
| 389 | findExpandedIndex(); |
| 390 | super.notifyDataSetChanged(); |
Adam Powell | 275702c | 2011-09-23 17:34:04 -0700 | [diff] [blame] | 391 | } |
Adam Powell | 11ed1d6 | 2011-07-11 21:19:59 -0700 | [diff] [blame] | 392 | } |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 393 | } |