BaseMenuPresenter.java revision 538e565c06e915b91e7e3a901f872ccdd9bccdd3
1/* 2 * Copyright (C) 2011 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 17package com.android.internal.view.menu; 18 19import android.content.Context; 20import android.view.LayoutInflater; 21import android.view.View; 22import android.view.ViewGroup; 23 24import java.util.ArrayList; 25 26/** 27 * Base class for MenuPresenters that have a consistent container view and item 28 * views. Behaves similarly to an AdapterView in that existing item views will 29 * be reused if possible when items change. 30 */ 31public abstract class BaseMenuPresenter implements MenuPresenter { 32 protected Context mSystemContext; 33 protected Context mContext; 34 protected MenuBuilder mMenu; 35 protected LayoutInflater mSystemInflater; 36 protected LayoutInflater mInflater; 37 private Callback mCallback; 38 39 private int mMenuLayoutRes; 40 private int mItemLayoutRes; 41 42 protected MenuView mMenuView; 43 44 private int mId; 45 46 /** 47 * Construct a new BaseMenuPresenter. 48 * 49 * @param context Context for generating system-supplied views 50 * @param menuLayoutRes Layout resource ID for the menu container view 51 * @param itemLayoutRes Layout resource ID for a single item view 52 */ 53 public BaseMenuPresenter(Context context, int menuLayoutRes, int itemLayoutRes) { 54 mSystemContext = context; 55 mSystemInflater = LayoutInflater.from(context); 56 mMenuLayoutRes = menuLayoutRes; 57 mItemLayoutRes = itemLayoutRes; 58 } 59 60 @Override 61 public void initForMenu(Context context, MenuBuilder menu) { 62 mContext = context; 63 mInflater = LayoutInflater.from(mContext); 64 mMenu = menu; 65 } 66 67 @Override 68 public MenuView getMenuView(ViewGroup root) { 69 if (mMenuView == null) { 70 mMenuView = (MenuView) mSystemInflater.inflate(mMenuLayoutRes, root, false); 71 mMenuView.initialize(mMenu); 72 updateMenuView(true); 73 } 74 75 return mMenuView; 76 } 77 78 /** 79 * Reuses item views when it can 80 */ 81 public void updateMenuView(boolean cleared) { 82 final ViewGroup parent = (ViewGroup) mMenuView; 83 if (parent == null) return; 84 85 int childIndex = 0; 86 if (mMenu != null) { 87 mMenu.flagActionItems(); 88 ArrayList<MenuItemImpl> visibleItems = mMenu.getVisibleItems(); 89 final int itemCount = visibleItems.size(); 90 for (int i = 0; i < itemCount; i++) { 91 MenuItemImpl item = visibleItems.get(i); 92 if (shouldIncludeItem(childIndex, item)) { 93 final View convertView = parent.getChildAt(childIndex); 94 final View itemView = getItemView(item, convertView, parent); 95 if (itemView != convertView) { 96 addItemView(itemView, childIndex); 97 } 98 childIndex++; 99 } 100 } 101 } 102 103 // Remove leftover views. 104 while (childIndex < parent.getChildCount()) { 105 if (!filterLeftoverView(parent, childIndex)) { 106 childIndex++; 107 } 108 } 109 } 110 111 /** 112 * Add an item view at the given index. 113 * 114 * @param itemView View to add 115 * @param childIndex Index within the parent to insert at 116 */ 117 protected void addItemView(View itemView, int childIndex) { 118 final ViewGroup currentParent = (ViewGroup) itemView.getParent(); 119 if (currentParent != null) { 120 currentParent.removeView(itemView); 121 } 122 ((ViewGroup) mMenuView).addView(itemView, childIndex); 123 } 124 125 /** 126 * Filter the child view at index and remove it if appropriate. 127 * @param parent Parent to filter from 128 * @param childIndex Index to filter 129 * @return true if the child view at index was removed 130 */ 131 protected boolean filterLeftoverView(ViewGroup parent, int childIndex) { 132 parent.removeViewAt(childIndex); 133 return true; 134 } 135 136 public void setCallback(Callback cb) { 137 mCallback = cb; 138 } 139 140 /** 141 * Create a new item view that can be re-bound to other item data later. 142 * 143 * @return The new item view 144 */ 145 public MenuView.ItemView createItemView(ViewGroup parent) { 146 return (MenuView.ItemView) mSystemInflater.inflate(mItemLayoutRes, parent, false); 147 } 148 149 /** 150 * Prepare an item view for use. See AdapterView for the basic idea at work here. 151 * This may require creating a new item view, but well-behaved implementations will 152 * re-use the view passed as convertView if present. The returned view will be populated 153 * with data from the item parameter. 154 * 155 * @param item Item to present 156 * @param convertView Existing view to reuse 157 * @param parent Intended parent view - use for inflation. 158 * @return View that presents the requested menu item 159 */ 160 public View getItemView(MenuItemImpl item, View convertView, ViewGroup parent) { 161 MenuView.ItemView itemView; 162 if (convertView instanceof MenuView.ItemView) { 163 itemView = (MenuView.ItemView) convertView; 164 } else { 165 itemView = createItemView(parent); 166 } 167 bindItemView(item, itemView); 168 return (View) itemView; 169 } 170 171 /** 172 * Bind item data to an existing item view. 173 * 174 * @param item Item to bind 175 * @param itemView View to populate with item data 176 */ 177 public abstract void bindItemView(MenuItemImpl item, MenuView.ItemView itemView); 178 179 /** 180 * Filter item by child index and item data. 181 * 182 * @param childIndex Indended presentation index of this item 183 * @param item Item to present 184 * @return true if this item should be included in this menu presentation; false otherwise 185 */ 186 public boolean shouldIncludeItem(int childIndex, MenuItemImpl item) { 187 return true; 188 } 189 190 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) { 191 if (mCallback != null) { 192 mCallback.onCloseMenu(menu, allMenusAreClosing); 193 } 194 } 195 196 public boolean onSubMenuSelected(SubMenuBuilder menu) { 197 if (mCallback != null) { 198 return mCallback.onOpenSubMenu(menu); 199 } 200 return false; 201 } 202 203 public boolean flagActionItems() { 204 return false; 205 } 206 207 public boolean expandItemActionView(MenuBuilder menu, MenuItemImpl item) { 208 return false; 209 } 210 211 public boolean collapseItemActionView(MenuBuilder menu, MenuItemImpl item) { 212 return false; 213 } 214 215 public int getId() { 216 return mId; 217 } 218 219 public void setId(int id) { 220 mId = id; 221 } 222} 223