Home | History | Annotate | Download | only in menu
      1 /*
      2  * Copyright (C) 2016 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 package androidx.appcompat.view.menu;
     17 
     18 import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
     19 
     20 import android.view.LayoutInflater;
     21 import android.view.View;
     22 import android.view.ViewGroup;
     23 import android.widget.BaseAdapter;
     24 
     25 import androidx.annotation.RestrictTo;
     26 
     27 import java.util.ArrayList;
     28 
     29 /**
     30  * @hide
     31  */
     32 @RestrictTo(LIBRARY_GROUP)
     33 public class MenuAdapter extends BaseAdapter {
     34     MenuBuilder mAdapterMenu;
     35 
     36     private int mExpandedIndex = -1;
     37 
     38     private boolean mForceShowIcon;
     39     private final boolean mOverflowOnly;
     40     private final LayoutInflater mInflater;
     41     private final int mItemLayoutRes;
     42 
     43     public MenuAdapter(MenuBuilder menu, LayoutInflater inflater, boolean overflowOnly,
     44             int itemLayoutRes) {
     45         mOverflowOnly = overflowOnly;
     46         mInflater = inflater;
     47         mAdapterMenu = menu;
     48         mItemLayoutRes = itemLayoutRes;
     49         findExpandedIndex();
     50     }
     51 
     52     public boolean getForceShowIcon() {
     53         return mForceShowIcon;
     54     }
     55 
     56     public void setForceShowIcon(boolean forceShow) {
     57         mForceShowIcon = forceShow;
     58     }
     59 
     60     @Override
     61     public int getCount() {
     62         ArrayList<MenuItemImpl> items = mOverflowOnly ?
     63                 mAdapterMenu.getNonActionItems() : mAdapterMenu.getVisibleItems();
     64         if (mExpandedIndex < 0) {
     65             return items.size();
     66         }
     67         return items.size() - 1;
     68     }
     69 
     70     public MenuBuilder getAdapterMenu() {
     71         return mAdapterMenu;
     72     }
     73 
     74     @Override
     75     public MenuItemImpl getItem(int position) {
     76         ArrayList<MenuItemImpl> items = mOverflowOnly ?
     77                 mAdapterMenu.getNonActionItems() : mAdapterMenu.getVisibleItems();
     78         if (mExpandedIndex >= 0 && position >= mExpandedIndex) {
     79             position++;
     80         }
     81         return items.get(position);
     82     }
     83 
     84     @Override
     85     public long getItemId(int position) {
     86         // Since a menu item's ID is optional, we'll use the position as an
     87         // ID for the item in the AdapterView
     88         return position;
     89     }
     90 
     91     @Override
     92     public View getView(int position, View convertView, ViewGroup parent) {
     93         if (convertView == null) {
     94             convertView = mInflater.inflate(mItemLayoutRes, parent, false);
     95         }
     96 
     97         final int currGroupId = getItem(position).getGroupId();
     98         final int prevGroupId =
     99                 position - 1 >= 0 ? getItem(position - 1).getGroupId() : currGroupId;
    100         // Show a divider if adjacent items are in different groups.
    101         ((ListMenuItemView) convertView)
    102                 .setGroupDividerEnabled(mAdapterMenu.isGroupDividerEnabled()
    103                         && (currGroupId != prevGroupId));
    104 
    105         MenuView.ItemView itemView = (MenuView.ItemView) convertView;
    106         if (mForceShowIcon) {
    107             ((ListMenuItemView) convertView).setForceShowIcon(true);
    108         }
    109         itemView.initialize(getItem(position), 0);
    110         return convertView;
    111     }
    112 
    113     void findExpandedIndex() {
    114         final MenuItemImpl expandedItem = mAdapterMenu.getExpandedItem();
    115         if (expandedItem != null) {
    116             final ArrayList<MenuItemImpl> items = mAdapterMenu.getNonActionItems();
    117             final int count = items.size();
    118             for (int i = 0; i < count; i++) {
    119                 final MenuItemImpl item = items.get(i);
    120                 if (item == expandedItem) {
    121                     mExpandedIndex = i;
    122                     return;
    123                 }
    124             }
    125         }
    126         mExpandedIndex = -1;
    127     }
    128 
    129     @Override
    130     public void notifyDataSetChanged() {
    131         findExpandedIndex();
    132         super.notifyDataSetChanged();
    133     }
    134 }