Android Custom Spinnerbutton
A myspinnerbutton that mimics the spinner button written by Android4.0. This can be achieved in the 1.6~4.0.4 version of this drop-down box effect.
Achieve effect: Click on the Spinner button on the screen, below the button will slide out a drop-down list, swipe to select one of the options in the drop-down list, the drop-down list is retracted, the text on the button appears as the content of the option. Click the Back button or drop-down list outside the drop-down list to retract. Repeatedly click the button, the drop-down list repeatedly perform the slide out and retract action.
Operation diagram:
Implementation principle: Use Popupwindow and button correlation (showasdropdown). Put the ListView on the Popupwindow. The result is a drop-down box.
Here's the code:
Entrance _04myspinnerdemo2activity:
public class _04myspinnerdemo2activity extends Activity {
Private Myspinnerbutton mspinnerbtn;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
THIS.MSPINNERBTN = (Myspinnerbutton) This.findviewbyid (R.ID.SPINNER_BTN);
}
}
Layout file Main.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
Android:gravity= "Top|center_horizontal"
android:orientation= "Vertical" >
<com.haozi.demo.myspinner2.wiget.myspinnerbutton
Android:id= "@+id/spinner_btn"
Android:layout_width= "200DP"
android:layout_height= "Wrap_content"
android:text= "Spinner Button"
/>
</LinearLayout>
Packaged Myspinnerbutton:
/**
* My Spinnerbutton.
* @author Haozi
*
*/
public class Myspinnerbutton extends Button {
Private context context;
Public Myspinnerbutton (context context, AttributeSet attrs, int defstyle) {
Super (context, attrs, Defstyle);
This.context = context;
Setting Up Listener Events
Setonclicklistener (New Myspinnerbuttononclicklistener ());
}
Public Myspinnerbutton (context context, AttributeSet Attrs) {
Super (context, attrs);
This.context = context;
Setting Up Listener Events
Setonclicklistener (New Myspinnerbuttononclicklistener ());
}
Public Myspinnerbutton (Context context) {
Super (context);
This.context = context;
Setting Up Listener Events
Setonclicklistener (New Myspinnerbuttononclicklistener ());
}
/**
* Myspinnerbutton Click events
* @author Haozi
*
*/
Class Myspinnerbuttononclicklistener implements view.onclicklistener{
@Override
public void OnClick (View v) {
Final Myspinnerdropdownitems mspinnerdropdrownitems = new Myspinnerdropdownitems (context);
if (!mspinnerdropdrownitems.isshowing ()) {
Mspinnerdropdrownitems.showasdropdown (Myspinnerbutton.this);
}
}
}
/**
* Myspinnerbutton drop-down list
* @author Haozi
*
*/
Class Myspinnerdropdownitems extends popupwindow{
Private context context;
Private LinearLayout mlayout; Layout of drop-down lists
Private ListView Mlistview; Drop-down list control
Private arraylist
Public Myspinnerdropdownitems (Context context) {
Super (context);
This.context = context;
Layout of drop-down lists
Mlayout = new LinearLayout (context);
Mlayout.setorientation (linearlayout.vertical);
Drop-down list control
Mlistview = new ListView (context);
Mlistview.setlayoutparams (New Layoutparams (MySpinnerButton.this.getLayoutParams (). width, layoutparams.wrap_ CONTENT));
Mlistview.setcachecolorhint (color.transparent);
Mdata = new arraylistfor (int i=0; i<50; i++) {
hashmap<string, string> mhashmap = new hashmap<string, string> ();
Mhashmap.put ("Spinner_dropdown_item_textview", "TextView" + i);
Mdata.add (MHASHMAP);
}
Setting up an adapter for a ListView
Mlistview.setadapter (New Myadapter (context,
Mdata, R.layout.spinner_dropdown_item,
New string[]{"Spinner_dropdown_item_textview"}, New Int[]{r.id.spinner_dropdown_item_textview});
Set Click events for a ListView
Mlistview.setonitemclicklistener (New Mylistviewonitemclickedlistener ());
Add the drop-down list to layout.
Mlayout.addview (Mlistview);
SetWidth (layoutparams.wrap_content);
SetHeight (layoutparams.wrap_content);
Setcontentview (mlayout);
Setfocusable (TRUE);
Mlayout.setfocusableintouchmode (TRUE);
}
/**
* My Adapter
* @author Haozi
*
*/
public class Myadapter extends Baseadapter {
Private context context;
Private list<? Extends Map<string,?>> Mdata;
private int mresource;
Private string[] Mfrom;
Private int[] MTo;
Private Layoutinflater Mlayoutinflater;
/**
* How to construct my adapter
* @param context of the caller of the context
* @param data
* @param Resource
* @param from
* @param to
*/
Public Myadapter (context context, list<?. extends Map<string,?>> data, int resource, string[] from, int[] to) {
This.context = context;
This.mdata = data;
This.mresource = resource;
This.mfrom = from;
This.mto = to;
This.mlayoutinflater = (layoutinflater) context.getsystemservice (context. Layout_inflater_service);
}
/**
* The system will call the GetCount method to get the item number before drawing the ListView
*/
public int GetCount () {
return This.mData.size ();
}
Public Object getItem (int position) {
return This.mData.get (position);
}
public long getitemid (int position) {
return position;
}
/**
* The GetView method is called once each item is drawn.
* In this method, you can refer to the pre-defined XML to determine the effect of the display and return a View object to be displayed as an item.
* It is also in this process to complete the main adapter conversion function, the data and resources to the developers want to show the effect.
* It is also the repeated invocation of GetView, making the use of the ListView simpler and more flexible.
* These two methods are the most important in customizing the ListView display, and as long as the two methods are rewritten, the ListView can be displayed exactly as the developer asks.
* The GetItem and Getitemid methods will be called when the ListView response method is called.
* So to ensure that the various methods of the ListView are valid, these two methods also have to be rewritten.
*/
Public View GetView (int position, View Contentview, ViewGroup parent) {
Contentview = This.mLayoutInflater.inflate (This.mresource, parent, false);
Set the content and style of the Contentview, where the emphasis is on setting the size of the text in Contentview
for (int index=0; index<this.mto.length; index++) {
TextView TextView = (TextView) Contentview.findviewbyid (This.mto[index]);
Textview.settext (This.mData.get (position). Get (This.mfrom[index]). toString ());
}
return contentview;
}
}
/**
* Click events for ListView
* @author Haozi
*
*/
Class Mylistviewonitemclickedlistener implements adapterview.onitemclicklistener{
@Override
public void Onitemclick (adapterview<?> parent, view view, int position,
Long id) {
TextView Mtextview = (TextView) View.findviewbyid (R.id.spinner_dropdown_item_textview);
String content = Mtextview.gettext (). toString ();
MySpinnerButton.this.setText (content);
MySpinnerDropDownItems.this.dismiss ();
}
}
}
}
Drop-down list item's layout file Spinner_dropdown_item.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:background= "@android:d rawable/list_selector_background"
android:orientation= "Vertical" >
<textview android:id= "@+id/spinner_dropdown_item_textview"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:gravity= "Left|bottom"
android:paddingtop= "11dip"
Android:paddingbottom= "11dip"
android:paddingleft= "5dip"
/>
</LinearLayout>
Android custom Spinnerbutton (RPM)