Android编程实现二级下拉菜单及快速搜索的方法
程序员文章站
2023-12-13 13:48:22
本文实例讲述了android编程实现二级下拉菜单及快速搜索的方法。分享给大家供大家参考,具体如下:
一、我们要做什么?
上面有个搜索框,下面是一个二级下拉菜单。...
本文实例讲述了android编程实现二级下拉菜单及快速搜索的方法。分享给大家供大家参考,具体如下:
一、我们要做什么?
上面有个搜索框,下面是一个二级下拉菜单。
输入查询内容,下面列表将显示查询结果。
二、界面设计
(1)这是主框架(部分属性已经省去,请看源码),从上至下分别是文本框,列表,二级列表。
<?xml version="1.0" encoding="utf-8"?> <linearlayout> <linearlayout android:id="@+id/city_middle"> <edittext android:id="@+id/txtfind" android:hint="请输入" > </edittext> <listview android:id="@+id/listfind" > </listview> <expandablelistview android:id="@+id/exlist" /> </linearlayout> </linearlayout>
(2)一级菜单栏样式,图片将区别是否展开
<?xml version="1.0" encoding="utf-8"?> <linearlayout > <textview android:id="@+id/group" > </textview> <imageview android:id="@+id/tubiao"> </imageview> </linearlayout>
(3)二级菜单栏样式
<?xml version="1.0" encoding="utf-8"?> <linearlayout > <textview android:id="@+id/child"> </textview> </linearlayout>
三、代码设计
(1) 定义菜单对应数据
public static list<basicnamevaluepair> fatherlist = new arraylist<basicnamevaluepair>(); public static list<list<basicnamevaluepair>> childlist = new arraylist<list<basicnamevaluepair>>();
生成测试数据
for (int i = 0; i < 20; i++) { fatherlist.add(new basicnamevaluepair("father" + i, "father" + i)); list<basicnamevaluepair> clist = new arraylist<basicnamevaluepair>(); for (int j = 0; j < 5; j++) { clist.add(new basicnamevaluepair("child" + i + ":" + j, "child" + i + ":" + j)); } childlist.add(clist); }
(2)定义列表适配器
protected class listadapter extends baseadapter { private layoutinflater minflater; //查询结果列表 private list<basicnamevaluepair> list = new arraylist<basicnamevaluepair>(); public listadapter(context context, string strin) { minflater = layoutinflater.from(context); //查询匹配 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; } }
初始化列表,默认为隐藏
list = (listview) findviewbyid(r.id.listfind); list.setvisibility(view.gone);
(3)定义二级列表适配器
protected class exadapter extends baseexpandablelistadapter { @override public int getgroupcount() { return fatherlist.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()); //展开,改变图片 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() { return true; } @override public boolean ischildselectable(int groupposition, int childposition) { return true; } }
初始化二级菜单
exlist = (expandablelistview) findviewbyid(r.id.exlist); exlist.setadapter(new exadapter()); exlist.setgroupindicator(null); exlist.setdivider(null);
(4)搜索事件,输入改变即触发
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, int 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)去除焦点自动弹出输入
getwindow().setsoftinputmode( windowmanager.layoutparams.soft_input_state_always_hidden);
希望本文所述对大家android程序设计有所帮助。
推荐阅读
-
Android编程实现二级下拉菜单及快速搜索的方法
-
Android编程实现启动另外的APP及传递参数的方法
-
Android编程实现设置按钮背景透明与半透明及图片背景透明的方法
-
Android编程实现获取标题栏、状态栏的高度、屏幕大小及模拟Home键的方法
-
Android编程实现全局获取Context及使用Intent传递对象的方法详解
-
Android编程实现获取系统内存、CPU使用率及状态栏高度的方法示例
-
Android编程实现启动另外的APP及传递参数的方法
-
Android编程实现两个Activity之间共享数据及互相访问的方法
-
android编程实现类似于支付宝余额快速闪动效果的方法
-
Android编程实现二级下拉菜单及快速搜索的方法