The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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 | |
| 19 | |
| 20 | import android.content.Context; |
| 21 | import android.content.res.TypedArray; |
| 22 | import android.util.AttributeSet; |
| 23 | import android.view.View; |
| 24 | import android.widget.AdapterView; |
| 25 | import android.widget.BaseAdapter; |
| 26 | import android.widget.ListAdapter; |
| 27 | import android.widget.ListView; |
| 28 | import android.widget.AdapterView.OnItemClickListener; |
| 29 | |
| 30 | import com.android.internal.view.menu.MenuBuilder.ItemInvoker; |
| 31 | |
| 32 | /** |
| 33 | * The expanded menu view is a list-like menu with all of the available menu items. It is opened |
| 34 | * by the user clicking no the 'More' button on the icon menu view. |
| 35 | */ |
| 36 | public final class ExpandedMenuView extends ListView implements ItemInvoker, MenuView, OnItemClickListener { |
| 37 | private MenuBuilder mMenu; |
| 38 | |
| 39 | /** Default animations for this menu */ |
| 40 | private int mAnimations; |
| 41 | |
| 42 | /** |
| 43 | * Instantiates the ExpandedMenuView that is linked with the provided MenuBuilder. |
| 44 | * @param menu The model for the menu which this MenuView will display |
| 45 | */ |
| 46 | public ExpandedMenuView(Context context, AttributeSet attrs) { |
| 47 | super(context, attrs); |
| 48 | |
| 49 | TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.MenuView, 0, 0); |
| 50 | mAnimations = a.getResourceId(com.android.internal.R.styleable.MenuView_windowAnimationStyle, 0); |
| 51 | a.recycle(); |
| 52 | |
| 53 | setOnItemClickListener(this); |
| 54 | } |
| 55 | |
| 56 | public void initialize(MenuBuilder menu, int menuType) { |
| 57 | mMenu = menu; |
| 58 | |
| 59 | setAdapter(menu.new MenuAdapter(menuType)); |
| 60 | } |
| 61 | |
| 62 | public void updateChildren(boolean cleared) { |
| 63 | ListAdapter adapter = getAdapter(); |
| 64 | // Tell adapter of the change, it will notify the mListView |
| 65 | if (adapter != null) { |
| 66 | if (cleared) { |
| 67 | ((BaseAdapter)adapter).notifyDataSetInvalidated(); |
| 68 | } |
| 69 | else { |
| 70 | ((BaseAdapter)adapter).notifyDataSetChanged(); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | protected void onDetachedFromWindow() { |
| 77 | super.onDetachedFromWindow(); |
| 78 | |
| 79 | // Clear the cached bitmaps of children |
| 80 | setChildrenDrawingCacheEnabled(false); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | protected boolean recycleOnMeasure() { |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | public boolean invokeItem(MenuItemImpl item) { |
| 89 | return mMenu.performItemAction(item, 0); |
| 90 | } |
| 91 | |
| 92 | public void onItemClick(AdapterView parent, View v, int position, long id) { |
| 93 | invokeItem((MenuItemImpl) getAdapter().getItem(position)); |
| 94 | } |
| 95 | |
| 96 | public int getWindowAnimations() { |
| 97 | return mAnimations; |
| 98 | } |
| 99 | |
| 100 | } |