18262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns/* 28262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * Copyright (C) 2013 The Android Open Source Project 38262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * 48262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * Licensed under the Apache License, Version 2.0 (the "License"); 58262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * you may not use this file except in compliance with the License. 68262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * You may obtain a copy of the License at 78262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * 88262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * http://www.apache.org/licenses/LICENSE-2.0 98262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * 108262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * Unless required by applicable law or agreed to in writing, software 118262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * distributed under the License is distributed on an "AS IS" BASIS, 128262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 138262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * See the License for the specific language governing permissions and 148262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * limitations under the License. 158262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns */ 168262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 1766698bb15ba0f873aa1c2290cc50d6bb839a474aChris Banespackage android.support.v7.view.menu; 188262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 198e10080c914d1ad0784394fa3026b85535535847Aurimas Liutikasimport static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP; 208e10080c914d1ad0784394fa3026b85535535847Aurimas Liutikas 218262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johnsimport android.content.Context; 228262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johnsimport android.os.Bundle; 238262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johnsimport android.os.Parcelable; 24c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viveretteimport android.support.annotation.RestrictTo; 25da10fdd1400ecfd8d7f2e55651dd528d0614dfc5Jeff Brownimport android.support.v7.appcompat.R; 268262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johnsimport android.util.SparseArray; 278262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johnsimport android.view.ContextThemeWrapper; 288262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johnsimport android.view.LayoutInflater; 298262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johnsimport android.view.View; 308262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johnsimport android.view.ViewGroup; 318262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johnsimport android.widget.AdapterView; 328262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johnsimport android.widget.BaseAdapter; 338262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johnsimport android.widget.ListAdapter; 348262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 358262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johnsimport java.util.ArrayList; 368262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 378262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns/** 388262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * MenuPresenter for list-style menus. 3989208232f3b5d1451408d787872504a190bc7ee0Chris Banes * 4089208232f3b5d1451408d787872504a190bc7ee0Chris Banes * @hide 418262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns */ 428e10080c914d1ad0784394fa3026b85535535847Aurimas Liutikas@RestrictTo(LIBRARY_GROUP) 438262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johnspublic class ListMenuPresenter implements MenuPresenter, AdapterView.OnItemClickListener { 448262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns private static final String TAG = "ListMenuPresenter"; 458262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 468262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns Context mContext; 478262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns LayoutInflater mInflater; 488262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns MenuBuilder mMenu; 498262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 508262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns ExpandedMenuView mMenuView; 518262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 522c1bad7ecd5879bf0f29ce2ce1bc5bd67a3f4682Aurimas Liutikas int mItemIndexOffset; 538262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns int mThemeRes; 548262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns int mItemLayoutRes; 558262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 568262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns private Callback mCallback; 578262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns MenuAdapter mAdapter; 588262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 598262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns private int mId; 608262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 618262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns public static final String VIEWS_TAG = "android:menu:list"; 628262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 638262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns /** 648262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * Construct a new ListMenuPresenter. 658262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * @param context Context to use for theming. This will supersede the context provided 668262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * to initForMenu when this presenter is added. 678262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * @param itemLayoutRes Layout resource for individual item views. 688262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns */ 698262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns public ListMenuPresenter(Context context, int itemLayoutRes) { 708262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns this(itemLayoutRes, 0); 718262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns mContext = context; 728262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns mInflater = LayoutInflater.from(mContext); 738262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 748262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 758262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns /** 768262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * Construct a new ListMenuPresenter. 778262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * @param itemLayoutRes Layout resource for individual item views. 788262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * @param themeRes Resource ID of a theme to use for views. 798262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns */ 808262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns public ListMenuPresenter(int itemLayoutRes, int themeRes) { 818262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns mItemLayoutRes = itemLayoutRes; 828262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns mThemeRes = themeRes; 838262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 848262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 858262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns @Override 868262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns public void initForMenu(Context context, MenuBuilder menu) { 878262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns if (mThemeRes != 0) { 888262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns mContext = new ContextThemeWrapper(context, mThemeRes); 898262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns mInflater = LayoutInflater.from(mContext); 908262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } else if (mContext != null) { 918262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns mContext = context; 928262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns if (mInflater == null) { 938262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns mInflater = LayoutInflater.from(mContext); 948262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 958262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 968262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns mMenu = menu; 978262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns if (mAdapter != null) { 988262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns mAdapter.notifyDataSetChanged(); 998262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 1008262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 1018262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 1028262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns @Override 1038262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns public MenuView getMenuView(ViewGroup root) { 10449c78900da0d43140fb602431fb93212bd7f6c70Chris Banes if (mMenuView == null) { 10549c78900da0d43140fb602431fb93212bd7f6c70Chris Banes mMenuView = (ExpandedMenuView) mInflater.inflate( 10649c78900da0d43140fb602431fb93212bd7f6c70Chris Banes R.layout.abc_expanded_menu_layout, root, false); 10749c78900da0d43140fb602431fb93212bd7f6c70Chris Banes if (mAdapter == null) { 10849c78900da0d43140fb602431fb93212bd7f6c70Chris Banes mAdapter = new MenuAdapter(); 1098262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 11049c78900da0d43140fb602431fb93212bd7f6c70Chris Banes mMenuView.setAdapter(mAdapter); 11149c78900da0d43140fb602431fb93212bd7f6c70Chris Banes mMenuView.setOnItemClickListener(this); 1128262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 11349c78900da0d43140fb602431fb93212bd7f6c70Chris Banes return mMenuView; 1148262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 1158262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 1168262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns /** 1178262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * Call this instead of getMenuView if you want to manage your own ListView. 1188262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * For proper operation, the ListView hosting this adapter should add 1198262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * this presenter as an OnItemClickListener. 1208262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * 1218262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * @return A ListAdapter containing the items in the menu. 1228262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns */ 1238262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns public ListAdapter getAdapter() { 1248262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns if (mAdapter == null) { 1258262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns mAdapter = new MenuAdapter(); 1268262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 1278262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns return mAdapter; 1288262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 1298262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 1308262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns @Override 1318262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns public void updateMenuView(boolean cleared) { 1328262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns if (mAdapter != null) mAdapter.notifyDataSetChanged(); 1338262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 1348262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 1358262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns @Override 1368262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns public void setCallback(Callback cb) { 1378262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns mCallback = cb; 1388262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 1398262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 1408262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns @Override 1418262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns public boolean onSubMenuSelected(SubMenuBuilder subMenu) { 1428262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns if (!subMenu.hasVisibleItems()) return false; 1438262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 1448262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns // The window manager will give us a token. 1458262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns new MenuDialogHelper(subMenu).show(null); 1468262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns if (mCallback != null) { 1478262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns mCallback.onOpenSubMenu(subMenu); 1488262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 1498262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns return true; 1508262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 1518262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 1528262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns @Override 1538262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) { 1548262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns if (mCallback != null) { 1558262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns mCallback.onCloseMenu(menu, allMenusAreClosing); 1568262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 1578262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 1588262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 1598262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns int getItemIndexOffset() { 1608262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns return mItemIndexOffset; 1618262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 1628262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 1638262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns public void setItemIndexOffset(int offset) { 1648262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns mItemIndexOffset = offset; 1658262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns if (mMenuView != null) { 1668262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns updateMenuView(false); 1678262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 1688262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 1698262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 1708262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns @Override 1718262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 17249c78900da0d43140fb602431fb93212bd7f6c70Chris Banes mMenu.performItemAction(mAdapter.getItem(position), this, 0); 1738262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 1748262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 1758262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns @Override 1768262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns public boolean flagActionItems() { 1778262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns return false; 1788262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 1798262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 180e2104f4b5c8e3ad63570306a25e61502dfe4c418Aurimas Liutikas @Override 1818262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns public boolean expandItemActionView(MenuBuilder menu, MenuItemImpl item) { 1828262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns return false; 1838262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 1848262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 185e2104f4b5c8e3ad63570306a25e61502dfe4c418Aurimas Liutikas @Override 1868262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns public boolean collapseItemActionView(MenuBuilder menu, MenuItemImpl item) { 1878262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns return false; 1888262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 1898262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 1908262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns public void saveHierarchyState(Bundle outState) { 1918262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns SparseArray<Parcelable> viewStates = new SparseArray<Parcelable>(); 1928262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns if (mMenuView != null) { 1938262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns ((View) mMenuView).saveHierarchyState(viewStates); 1948262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 1958262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns outState.putSparseParcelableArray(VIEWS_TAG, viewStates); 1968262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 1978262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 1988262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns public void restoreHierarchyState(Bundle inState) { 1998262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns SparseArray<Parcelable> viewStates = inState.getSparseParcelableArray(VIEWS_TAG); 2008262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns if (viewStates != null) { 2018262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns ((View) mMenuView).restoreHierarchyState(viewStates); 2028262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 2038262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 2048262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 2058262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns public void setId(int id) { 2068262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns mId = id; 2078262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 2088262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 2098262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns @Override 2108262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns public int getId() { 2118262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns return mId; 2128262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 2138262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 2148262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns @Override 2158262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns public Parcelable onSaveInstanceState() { 2168262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns if (mMenuView == null) { 2178262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns return null; 2188262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 2198262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 2208262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns Bundle state = new Bundle(); 2218262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns saveHierarchyState(state); 2228262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns return state; 2238262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 2248262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 2258262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns @Override 2268262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns public void onRestoreInstanceState(Parcelable state) { 2278262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns restoreHierarchyState((Bundle) state); 2288262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 2298262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 2308262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns private class MenuAdapter extends BaseAdapter { 2318262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns private int mExpandedIndex = -1; 2328262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 2338262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns public MenuAdapter() { 2348262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns findExpandedIndex(); 2358262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 2368262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 237e2104f4b5c8e3ad63570306a25e61502dfe4c418Aurimas Liutikas @Override 2388262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns public int getCount() { 2398262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns ArrayList<MenuItemImpl> items = mMenu.getNonActionItems(); 2408262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns int count = items.size() - mItemIndexOffset; 2418262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns if (mExpandedIndex < 0) { 2428262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns return count; 2438262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 2448262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns return count - 1; 2458262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 2468262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 247e2104f4b5c8e3ad63570306a25e61502dfe4c418Aurimas Liutikas @Override 2488262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns public MenuItemImpl getItem(int position) { 2498262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns ArrayList<MenuItemImpl> items = mMenu.getNonActionItems(); 2508262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns position += mItemIndexOffset; 2518262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns if (mExpandedIndex >= 0 && position >= mExpandedIndex) { 2528262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns position++; 2538262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 2548262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns return items.get(position); 2558262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 2568262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 257e2104f4b5c8e3ad63570306a25e61502dfe4c418Aurimas Liutikas @Override 2588262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns public long getItemId(int position) { 2598262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns // Since a menu item's ID is optional, we'll use the position as an 2608262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns // ID for the item in the AdapterView 2618262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns return position; 2628262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 2638262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 264e2104f4b5c8e3ad63570306a25e61502dfe4c418Aurimas Liutikas @Override 2658262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns public View getView(int position, View convertView, ViewGroup parent) { 2668262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns if (convertView == null) { 2678262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns convertView = mInflater.inflate(mItemLayoutRes, parent, false); 2688262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 2698262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 2708262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns MenuView.ItemView itemView = (MenuView.ItemView) convertView; 2718262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns itemView.initialize(getItem(position), 0); 2728262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns return convertView; 2738262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 2748262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 2758262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns void findExpandedIndex() { 2768262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns final MenuItemImpl expandedItem = mMenu.getExpandedItem(); 2778262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns if (expandedItem != null) { 2788262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns final ArrayList<MenuItemImpl> items = mMenu.getNonActionItems(); 2798262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns final int count = items.size(); 2808262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns for (int i = 0; i < count; i++) { 2818262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns final MenuItemImpl item = items.get(i); 2828262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns if (item == expandedItem) { 2838262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns mExpandedIndex = i; 2848262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns return; 2858262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 2868262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 2878262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 2888262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns mExpandedIndex = -1; 2898262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 2908262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns 2918262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns @Override 2928262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns public void notifyDataSetChanged() { 2938262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns findExpandedIndex(); 2948262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns super.notifyDataSetChanged(); 2958262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 2968262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns } 2978262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns} 298