This article describes the Android programming implementation of the two-level drop-down menu and fast search methods. Share to everyone for your reference, specific as follows:
A, what are we going to do?
There is a search box, below is a level two Drop-down menu.
Enter the contents of the query, and the following list displays the results of the query.
Second, interface design
(1) This is the main frame (some of the properties have been omitted, please see the source), from top to bottom are text boxes, lists, level two list.
<?xml version= "1.0" encoding= "Utf-8"?>
<LinearLayout>
<linearlayout android:id=
"@+id/ City_middle ">
<edittext
android:id=" @+id/txtfind
android:hint= "Please enter" >
</edittext >
<listview
android:id= "@+id/listfind" >
</ListView>
<expandablelistview
android:id= "@+id/exlist"/>
</LinearLayout>
</LinearLayout>
(2) First-level menu bar style, the picture will distinguish whether to expand
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout >
<textview android:id=
"@+id/ Group ">
</TextView>
<imageview
android:id=" @+id/tubiao ">
</ImageView>
</LinearLayout>
(3) Level two menu bar style
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout >
<textview android:id=
"@+id/ Child ">
</TextView>
</LinearLayout>
Third, code design
(1) Define menu corresponding data
public static list<basicnamevaluepair> fatherlist = new arraylist<basicnamevaluepair> ();
public static list<list<basicnamevaluepair>> childlist = new arraylist<list<basicnamevaluepair> > ();
Generating test data
for (int i = 0; i < i++) {
fatherlist.add (new Basicnamevaluepair ("Father" + I, "father" + i));
list<basicnamevaluepair> cList = new arraylist<basicnamevaluepair> ();
for (int j = 0; J < 5; J +) {
Clist.add (the New Basicnamevaluepair ("child" + i + ":" + J, "Child"
+ i + ":" + j)); c6/>}
Childlist.add (cList);
}
(2) Defining list Adapters
Protected class ListAdapter extends Baseadapter {private Layoutinflater minflater;
Query results list private list<basicnamevaluepair> list = new arraylist<basicnamevaluepair> ();
Public ListAdapter (context context, String Strin) {minflater = Layoutinflater.from (context);
Query matching for (int i = 0; i < childlist.size (); i++) {for (int j = 0; J < Childlist.get (i). Size (); j + +) {
String tmp = Childlist.get (i). Get (J). GetValue ();
if (Tmp.indexof (Strin) >= 0) {list.add (New Basicnamevaluepair (Childlist.get (i). Get (J). GetName (), TMP));
'}}} public int GetCount () {return list.size ();
Public Object getitem (int position) {return position;
public long getitemid (int position) {return position; Public View GetView (final int position, View Convertview, ViewGroup parent) {Convertview = Minflater.inflate (R
. Layout.child, NULL); TextView title = (TextView) Convertview.findviewbyid (R.id.chilD);
Title.settext (List.get (position). GetValue ());
return convertview;
}
}
Initialize the list, default to hidden
List = (ListView) Findviewbyid (r.id.listfind);
List.setvisibility (View.gone);
(3) Define a level two list adapter
Protected class Exadapter extends Baseexpandablelistadapter {@Override public int getgroupcount () {return father
List.size ();
@Override public int Getchildrencount (int. groupposition) {return childlist.get (groupposition). Size ();
@Override public Object getgroup (int groupposition) {return fatherlist.get (groupposition). GetValue (); @Override public Object getchild (int groupposition, int childposition) {return childlist.get (groupposition). Get (
childposition). GetValue ();
@Override public long getgroupid (int groupposition) {return groupposition;
@Override public long Getchildid (int groupposition, int childposition) {return childposition;
@Override public View getgroupview (int groupposition, Boolean isexpanded, View Convertview, ViewGroup parent) {
View view = Convertview;
if (view = = null) {Layoutinflater Inflater = (layoutinflater) getsystemservice (Context.layout_inflater_service); View = Inflater.Inflate (r.layout.group, NULL);
} TextView t = (TextView) View.findviewbyid (R.id.group);
T.settext (Fatherlist.get (groupposition). GetValue ());
Expand, change picture ImageView gimg = (imageview) View.findviewbyid (R.id.tubiao);
if (isexpanded) Gimg.setbackgroundresource (r.drawable.mm_submenu_down_normal);
else Gimg.setbackgroundresource (r.drawable.mm_submenu_normal);
return view; @Override public View getchildview (int groupposition, int childposition, Boolean islastchild, View Convertview,
ViewGroup parent) {View view = Convertview;
if (view = = null) {Layoutinflater Inflater = (layoutinflater) getsystemservice (Context.layout_inflater_service);
View = Inflater.inflate (R.layout.child, NULL);
} TextView t = (TextView) View.findviewbyid (r.id.child);
T.settext (Childlist.get (groupposition). Get (Childposition). GetValue ());
return view;
@Override public boolean Hasstableids () {true; } @Override Public Boolean ischildselectable (int groupposition, int childposition) {return true;
}
}
Initialize level two menu
Exlist = (Expandablelistview) Findviewbyid (r.id.exlist);
Exlist.setadapter (New Exadapter ());
Exlist.setgroupindicator (null);
Exlist.setdivider (NULL);
(4) Search event, input change is triggered
Txtfind = (edittext) Findviewbyid (r.id.txtfind);
Txtfind.addtextchangedlistener (New Textwatcher () {
@Override public
void beforetextchanged (Charsequence s, int start, int count,
int after) {
}
@Override public
void ontextchanged (charsequence s, int start, T before,
int count) {
}
@Override public
void aftertextchanged (Editable s) {
if (s!= null ;&!s.tostring (). Equals ("")) {
list.setadapter (new ListAdapter (Dwinterdemoactivity.this, S
. toString ()));
List.setvisibility (view.visible);
Exlist.setvisibility (View.gone);
} else {
list.setvisibility (view.gone);
Exlist.setvisibility (view.visible);
}
}
);
(5) Remove focus automatically eject input
GetWindow (). Setsoftinputmode (
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
I hope this article will help you with the Android program.