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