欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android编程实现二级下拉菜单及快速搜索的方法

程序员文章站 2023-12-13 13:48:22
本文实例讲述了android编程实现二级下拉菜单及快速搜索的方法。分享给大家供大家参考,具体如下: 一、我们要做什么? 上面有个搜索框,下面是一个二级下拉菜单。...

本文实例讲述了android编程实现二级下拉菜单及快速搜索的方法。分享给大家供大家参考,具体如下:

一、我们要做什么?

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程序设计有所帮助。

上一篇:

下一篇: