The custom variables are as follows:
Private Context mcontext;private layoutinflater minflater;private popupwindow mpopupwindow;private ListView MListView; Private Customspinneradapter madapter;//Lahan Adapter private onspinneritemselectedlistener mlistener;//Custom Check callback interface private list<spinneritem> mspinneritems;//Custom Data sets
The next Lahan adapter is primarily used to display the drop-down text with the "check icon" after the selected text, and the custom check drop-down item callback interface is as follows:
/** * Item Check Callback interface * @author liuyinjun* @date 2015-2-8 */public interface Onspinneritemselectedlistener {public void Onitems Elected (Spinneritem item);}
To facilitate unified display, the custom data type Spinneritem:
/** * * @author liuyinjun* @date 2015-2-8 */public class Spinneritem {public int id; public String name;//display name public bool Ean ischoose;//is currently selected}
Initialize Popupwindow:
/** * initializing Data */ private void initdata () { // initialization popupwindow mpopupwindow = new popupwindow (Mcontext); // The touch control disappears outside the mpopupwindow.settouchinterceptor (New ontouchlistener () { @ Override public boolean ontouch (view v, motionevent event) { if (Event.getaction () == motionevent.action_outside) { mpopupwindow.dismiss (); return true; } return false; } }); View contentview = minflater.inflate (r.layout.spinner_shared_list, null); mpopupwindow.setcontentview (Contentview); mpopupwindow.setwidth (LayoutParams.FILL_ PARENT); mpopupwindow.setheighT (layoutparams.wrap_content); mpopupwindow.settouchable (true); mpopupwindow.setfocusable (true); mpopupwindow.setoutsidetouchable (true); mpopupwindow.setbackgrounddrawable (new bitmapdrawable ()); mpopupwindow.setanimationstyle (r.style.spinneranimation);// set enter and exit animation mListView = (ListView) contentview.findviewbyid (R.id.popwin_shared_listview); mlistview.setonitemclicklistener (New onitemclicklistener () {// drop down after clicking // item Events @Override public void onitemclick (adapterview <?> parent, view view, int position, long id) { if (mlistener != null) { Spinneritem item&nBsp;= mspinneritems.get (position); setchoose (item.id); mlistener.onitemselected (item); } mpopupwindow.dismiss (); } }) ; madapter = new customspinneradapter (Mcontext, mspinneritems); Mlistview.setadapter (madapter); }/** * Select item and notify the adapter to change the display * * @param id */ public void setchoose (int id) { LOG.I ("id=", "" + id); for (Spinneritem item : mspinneritems) { if (Item.id == id) { item.isChoose = true; } else { item.ischoose = false; } } madapter.noTifydatasetchanged (); }
The
Increases the dataset in 3 ways:
/** * Method 1: Add Datasets * * @param based on the list collection object * ,object must implement the ToString method */ public <t> void add (List<t> object) { if (object == null) return; final int length = object.size (); for (int i = 0; i < length; i++) { add (I, object.get (i). toString ()); } } /** * Method 2: Increase data set by parameter * * @param names */ public void add (String ... names) { if (names == null) return; final int length = names.length; for (int i = 0; i < length; i++) {&Nbsp; add (I, names[i]); } } /** * Method 3: Add datasets based on resource ID * * @param id * @param nameres */ public void add (int id, int nameres) { add (id, mContext.getString ( Nameres)); } /** * converted to a custom data type Spinneritem, adding the dataset to the mspinneritems * * @param id * @param name */ public void add (Int id, string name) { SpinnerItem item = new Spinneritem (); item.id = id; item.name = name; Mspinneritems.add (item); }
Drop-down display:
/*** drop-down display * * @param anchor*/public void Show (View anchor) {Mpopupwindow.showasdropdown (anchor, 0, 0);}
At this point, the key code has been posted, the key is the use of Popupwindow, about how to use see Custom display multi-line Spinner
Customizing to display multiple lines Spinner