/* * Copyright (C) 2011 Google Inc. * Licensed to The Android Open Source Project. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.mailcommon; import com.android.mailcommon.MergedAdapter.ListSpinnerAdapter; import com.android.mailcommon.MergedAdapter.LocalAdapterPosition; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.FrameLayout; import android.widget.ListPopupWindow; import android.widget.ListView; /** *
A spinner-like widget that combines data and views from multiple adapters (via MergedAdapter) * and forwards certain events to those adapters. This widget also supports clickable but * unselectable dropdown items, useful when displaying extra items that should not affect spinner * selection state.
* *The framework's Spinner widget can't be extended for this task because it uses a private list * adapter (which prevents setting multiple item types) and hides access to its popup, which is * useful for clients to know about (like when it's been opened).
* *Clients must provide a set of adapters which the widget will use to load dropdown views, * receive callbacks, and load the selected item's view.
* *Apps incorporating this widget must declare a custom attribute: "dropDownWidth" under the * "MultiAdapterSpinner" name as a "reference" format (see Gmail's attrs.xml file for an * example). This attribute controls the width of the dropdown, similar to the attribute in the * framework's Spinner widget.
* */ public class MultiAdapterSpinner extends FrameLayout implements AdapterView.OnItemClickListener, View.OnClickListener { protected MergedSpinnerAdapter mAdapter; protected ListPopupWindow mPopup; private int mSelectedPosition = -1; /** * A basic adapter with some callbacks added so clients can be involved in spinner behavior. */ public interface FancySpinnerAdapter extends ListSpinnerAdapter { /** * Whether or not an item at position should become the new selected spinner item and change * the spinner item view. */ boolean canSelect(int position); /** * Handle a click on an enabled item. */ void onClick(int position); /** * Fired when the popup window is about to be displayed. */ void onShowPopup(); } private class MergedSpinnerAdapter extends MergedAdapter