Custom drop-down style spinner in Android
This article continues with the introduction of the Android custom control family, customizing the use of the Spinner control.
Implementation ideas
1. Define the drop-down control layout (ListView and child control layouts)
2. Customizing the Spinerpopwindow class
3. Define the adapter of the fill data
I. Defining the layout of controls
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:background=" #f2f2f2 " android:orientation= "vertical" android:padding= "5DP" > <linearlayout android:id= "@+id/layout" a Ndroid:layout_width= "Match_parent" android:layout_height= "wrap_content" android:layout_margin= "5DP" a Ndroid:background= "#cbcbcb" android:orientation= "vertical" android:padding= "0.2px" > <relativel Ayout android:layout_width= "wrap_content" android:layout_height= "42DP" > <textview Android:id= "@+id/tv_value" android:layout_width= "Match_parent" android:layout _height= "Match_parent" android:layout_centervertical= "true" android:layout_marginright= "50DP "Android:backgrouNd= "#fff" android:ellipsize= "End" android:gravity= "Left|center" android:hint= "Please select" android:paddingleft= "10DP" android:singleline= "true" android:textcolor= "#ff000000" android:textsize= "18DP" > </TextView> <linearlayout Android:id= "@+id/bt_dropdown" android:layout_width= "50DP" android:layout_height= "match _parent "android:layout_alignparentright=" true "android:background=" #fff "and" roid:gravity= "center" android:onclick= "OnClick" > <imageview androi D:layout_width= "20DP" android:layout_height= "15DP" android:background= "@drawable/dow N_arrow "/> </LinearLayout> </RelativeLayout> </linearlayout></relativelayout >
Second, define the Spinerpopwindow class
Package Org.gaochun.widget;import Java.util.list;import Org.gaochun.r;import org.gaochun.adapter.SpinerAdapter; Import Org.gaochun.adapter.spineradapter.ionitemselectlistener;import Android.content.context;import Android.graphics.drawable.colordrawable;import Android.view.layoutinflater;import Android.view.View;import Android.view.viewgroup.layoutparams;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.listview;import Android.widget.PopupWindow /** * Custom Spinerpopwindow class * @author Gao_chun * */public class Spinerpopwindow extends Popupwindow implements Onitemclickl istener{private Context Mcontext; Private ListView Mlistview; Private Spineradapter Madapter; Private Ionitemselectlistener Mitemselectlistener; Public Spinerpopwindow (Context context) {super (context); Mcontext = context; Init (); } public void Setitemlistener (Ionitemselectlistener listener) {Mitemselectlistener = LiStener; } public void Setadatper (Spineradapter adapter) {Madapter = adapter; Mlistview.setadapter (Madapter); } private void Init () {View view = Layoutinflater.from (Mcontext). Inflate (R.layout.spiner_window_layout, NULL ); Setcontentview (view); SetWidth (layoutparams.wrap_content); SetHeight (layoutparams.wrap_content); Setfocusable (TRUE); Colordrawable DW = new colordrawable (0x00); Setbackgrounddrawable (DW); Mlistview = (ListView) View.findviewbyid (R.id.listview); Mlistview.setonitemclicklistener (this); } public void RefreshData (list<string> list, int selindex) {if (list! = NULL && selindex! =- 1) {if (madapter! = null) {Madapter.refreshdata (list, selindex); }}} @Override public void Onitemclick (adapterview<?> arg0, view view, int pos, long arg3) { Dismiss (); if (Mitemselectlistener! = NULL) {Mitemselectlistener.onitemclick (POS); } }}
iii. definition of Adapter
Package Org.gaochun.adapter;import Java.util.list;import Org.gaochun.r;import android.content.context;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.baseadapter;import Android.widget.textview;public class Spineradapter extends BaseAdapter {public stat IC interface ionitemselectlistener{public void Onitemclick (int pos); }; Private list<string> mobjects; Private Layoutinflater Minflater; Public Spineradapter (Context context,list<string> mobjects) {this.mobjects = mobjects; Minflater = (layoutinflater) context.getsystemservice (Context.layout_inflater_service); } public void RefreshData (list<string> objects, int selindex) {mobjects = objects; if (Selindex < 0) {Selindex = 0; } if (Selindex >= mobjects.size ()) {Selindex = Mobjects.size ()-1; }} @Override public int getcount () {return mObJects.size (); } @Override public Object getItem (int pos) {return Mobjects.get (POS). toString (); } @Override public long getitemid (int pos) {return pos; } @Override public View getView (int pos, View convertview, ViewGroup arg2) {Viewholder viewholder; if (Convertview = = null) {Convertview = minflater.inflate (r.layout.spiner_item_layout, NULL); Viewholder = new Viewholder (); Viewholder.mtextview = (TextView) Convertview.findviewbyid (R.id.textview); Convertview.settag (Viewholder); } else {Viewholder = (Viewholder) convertview.gettag (); }//object item = GetItem (POS); ViewHolder.mTextView.setText (Mobjects.get (POS)); return convertview; } public static class Viewholder {public TextView mtextview; }}
Iv. Invocation of the sample
public class Mainactivity extends Activity implements onclicklistener,spineradapter.ionitemselectlistener{private Lis t<string> Mlisttype = new arraylist<string> (); Type list private TextView Mtview; Private Spineradapter Madapter; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_search); Mtview = (TextView) Findviewbyid (R.id.tv_value); Initialize data string[] names = Getresources (). Getstringarray (R.array.array_name); for (int i = 0; i < names.length; i++) {Mlisttype.add (names[i]); } madapter = new Spineradapter (this,mlisttype); Madapter.refreshdata (mlisttype,0); Displays the first Data//mtview.settext (Names[0]); Initialize Popwindow Mspinerpopwindow = new Spinerpopwindow (this); Mspinerpopwindow.setadatper (Madapter); Mspinerpopwindow.setitemlistener (this); }//Set Popwindow private Spinerpopwindow Mspinerpopwindow; private void Showspinwindow () {LOG.E ("", "Showspinwindow"); Mspinerpopwindow.setwidth (Mtview.getwidth ()); Mspinerpopwindow.showasdropdown (Mtview); } @Override public void OnClick (View v) {switch (V.getid ()) {r.id.bt_dropdown: Showspinwindow (); Break }//* (non-javadoc) * @see org.gaochun.adapter.spineradapter.ionitemselectlistener#onitemclick (int) */@ Override public void Onitemclick (int pos) {//TODO auto-generated Method stub if (pos >= 0 && POS <= mlisttype.size ()) {String value = Mlisttype.get (POS); Mtview.settext (Value.tostring ()); } }}
Source Download: http://download.csdn.net/download/gao_chun/8653069
Five, custom control series
using custom progress Loading in Android Projects dialog:http://blog.csdn.net/gao_chun/article/details/45270031
button Listener Setup and optimization in ListView:http://blog.csdn.net/gao_chun/article/details/41249131
Custom top title bar in Android Project:http://blog.csdn.net/gao_chun/article/details/45255929
Android Custom settings round picture control:http://blog.csdn.net/gao_chun/article/details/39207557
Android Custom Bottom tab, project overall interface frame:http://blog.csdn.net/gao_chun/article/details/37903673
Custom Alertdialog Prompt box: http://blog.csdn.net/gao_chun/article/details/37757571
Reproduced in the source.
Custom drop-down style spinner in Android