Android編程實(shí)現(xiàn)二級(jí)下拉菜單及快速搜索的方法
本文實(shí)例講述了Android編程實(shí)現(xiàn)二級(jí)下拉菜單及快速搜索的方法。分享給大家供大家參考,具體如下:
一、我們要做什么?

上面有個(gè)搜索框,下面是一個(gè)二級(jí)下拉菜單。

輸入查詢內(nèi)容,下面列表將顯示查詢結(jié)果。
二、界面設(shè)計(jì)
(1)這是主框架(部分屬性已經(jīng)省去,請(qǐng)看源碼),從上至下分別是文本框,列表,二級(jí)列表。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout> <LinearLayout android:id="@+id/city_middle"> <EditText android:id="@+id/txtfind" android:hint="請(qǐng)輸入" > </EditText> <ListView android:id="@+id/listfind" > </ListView> <ExpandableListView android:id="@+id/exList" /> </LinearLayout> </LinearLayout>
(2)一級(jí)菜單欄樣式,圖片將區(qū)別是否展開
<?xml version="1.0" encoding="utf-8"?> <LinearLayout > <TextView android:id="@+id/group" > </TextView> <ImageView android:id="@+id/tubiao"> </ImageView> </LinearLayout>
(3)二級(jí)菜單欄樣式
<?xml version="1.0" encoding="utf-8"?> <LinearLayout > <TextView android:id="@+id/child"> </TextView> </LinearLayout>
三、代碼設(shè)計(jì)
(1) 定義菜單對(duì)應(yīng)數(shù)據(jù)
public static List<BasicNameValuePair> fatherList = new ArrayList<BasicNameValuePair>(); public static List<List<BasicNameValuePair>> childList = new ArrayList<List<BasicNameValuePair>>();
生成測(cè)試數(shù)據(jù)
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;
//查詢結(jié)果列表
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;
}
}
初始化列表,默認(rèn)為隱藏
list = (ListView) findViewById(R.id.listfind); list.setVisibility(View.GONE);
(3)定義二級(jí)列表適配器
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;
}
}
初始化二級(jí)菜單
exList = (ExpandableListView) findViewById(R.id.exList); exList.setAdapter(new ExAdapter()); exList.setGroupIndicator(null); exList.setDivider(null);
(4)搜索事件,輸入改變即觸發(fā)
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)去除焦點(diǎn)自動(dòng)彈出輸入
getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android自定義View實(shí)現(xiàn)圓弧進(jìn)度的效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)圓弧進(jìn)度的效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01
Android中搜索圖標(biāo)和文字居中的EditText實(shí)例
本篇文章主要介紹了Android中搜索圖標(biāo)和文字居中的EditText實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下2017-06-06
Android高級(jí)開發(fā)之性能優(yōu)化典范
本文從電量,視圖,內(nèi)存三個(gè)性能方面的知識(shí)點(diǎn)給大家介紹android高級(jí)開發(fā)之性能優(yōu)化的相關(guān)知識(shí),希望對(duì)大家有所幫助2016-05-05
Android開發(fā)之Notification通知用法詳解
這篇文章主要介紹了Android開發(fā)之Notification通知用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Notification通知的功能、參數(shù)、定義及使用方法,需要的朋友可以參考下2016-11-11
Android Compose 屬性動(dòng)畫使用探索詳解
這篇文章主要為大家介紹了Android Compose 屬性動(dòng)畫使用探索詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
解決Android中自定義DialogFragment解決寬度和高度問題
Android中自定義DialogFragment解決寬度和高度問題但是我們很多時(shí)候想把DialogFragment的高度固定,那么我們需要設(shè)置DialogFragment的高度,在Fragment的onResume()聲明周期方法中設(shè)置window的寬高即可2017-12-12
Android中EditText光標(biāo)在4.0中的bug及解決方法
這篇文章主要介紹了Android中EditText光標(biāo)在4.0中的bug及解決方法,簡(jiǎn)單分析了Android4.0版本中EditText光標(biāo)消息的原因及相應(yīng)的解決方法,需要的朋友可以參考下2016-01-01
Android AsyncTask完全解析 帶你從源碼的角度徹底理解
這篇文章主要是針對(duì)Android AsyncTask進(jìn)行完全解析,帶你從源碼的角度徹底理解,感興趣的小伙伴們可以參考一下2016-04-04

