Look at the UI effect first
This is implemented using Popupwindow nested listview, this simple, not long introduction directly on the code
1: Layout file
<span style= "FONT-SIZE:18PX;" ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " > <textview android:id= "@+id/tv_text_age" android:layout_width= "Wrap_content" android: layout_height= "Wrap_content" android:background= "@drawable/choolse_bg" android:text= "unlimited" android: paddingleft= "12DP" android:textcolor= "#9d72d2" android:layout_centerinparent= "true" Android: Gravity= "Center_vertical" /></relativelayout></span>
The background image is as follows:
Package Com.example.demo;import Java.util.arraylist;import Java.util.list;import android.app.activity;import Android.graphics.color;import Android.graphics.drawable.colordrawable;import Android.os.Bundle;import Android.view.layoutinflater;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.viewgroup;import Android.view.viewgroup.layoutparams;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.baseadapter;import Android.widget.ListView; Import Android.widget.popupwindow;import Android.widget.textview;public class Mainactivity extends Activity Implements Onclicklistener {private TextView tv_text_age;private Popupwindow pop; ListView ListView = Null;private list<string> datas;private selectageadapter mselectadapter;private Layoutinflater inflater; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); InitData (); inflAter = Layoutinflater.from (this); tv_text_age = (TextView) Findviewbyid (r.id.tv_text_age); tv_text_ Age.setonclicklistener (this);} private void InitData () {datas = new arraylist<string> (); for (int i=0;i<50;i++) {datas.add ("helloworld-" +i);}} @Overridepublic void OnClick (View v) {if (V.getid () ==r.id.tv_text_age) {if (pop = = null) {ListView = new ListView (this); Listview.setdividerheight (1); Listview.setbackgroundresource (R.DRAWABLE.KGE_FEEK_BG); listView.setCacheColorHint (0x00000000); mselectadapter = new Selectageadapter ();p op = new Popupwindow (ListView, Tv_text_age.getwidth (), Layoutparams.wrap_content, True);} Listview.setadapter (Mselectadapter);p op.setbackgrounddrawable (New colordrawable (color.red));// Pop.showasdropdown (tv_text_age, 0, 0); Listview.setonitemclicklistener (new Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview<?> parent, View view,int position, long id) {Tv_text_age.settext (Datas.get (position ));d Ismisspopwindow ();}});}} public void Dismisspopwindow () {if (Pop!=null&&pop.isshowing ()) {Pop.dismiss ();}} Class Selectageadapter extends baseadapter{@Overridepublic int GetCount () {return datas!=null&&datas.size () >0?datas.size (): 0;} @Overridepublic Object getItem (int position) {return datas.get (position);} @Overridepublic long Getitemid (int position) {return position;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {Viewholder holder;if (convertview==null) {holder = new Viewholder (); Convertview = Inflater.inflate (R.layout.item, null); holder.tv_name = (TextView) Convertview.findviewbyid (R.id.tv_name); Convertview.settag (holder);} Else{holder = (Viewholder) Convertview.gettag ();} Holder.tv_name.setText (Datas.get (position)); return Convertview;}} Class Viewholder{textview tv_name;}}
Implementation results:
Android Popupwindow nested listview (Mimic Category drop-down menu effect)