上一次直接使用的是arrayadapter 以及 系统自带的simple_list1布局文件.但是开发中,我们基本是不会用到那么简单的cell的,下面就来说说如何定制自己的过滤器,让他想搜索什么内容就搜索什么内容
效果图:
listview_cell布局:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <ImageView android:id="@+id/iv_logo" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:src="@drawable/ic_launcher" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="50dp" android:orientation="vertical" > <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="name" android:layout_weight="1" /> <TextView android:id="@+id/tv_show" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我就是我不一样的烟火" android:layout_weight="1" /> </LinearLayout></LinearLayout>
xml:
<LinearLayout 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" android:orientation="vertical" tools:context="com.example.testedittextlistview.MainActivity" > <include layout="@layout/myedittext" /> <ListView android:id="@+id/lv_main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000" > </ListView></LinearLayout>
listview_adapter:
package com.example.testedittextlistview;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import android.content.Context;import android.text.TextUtils;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Filter;import android.widget.ImageView;import android.widget.TextView;public class MyListViewAdapter extends BaseAdapter { //这个是我们数据的总集合,在过滤的时候,这个应该是不变的 private List<People> myforeverlist; //上下文菜单 private Context context; //这个是,过滤的时候记载满足要求的那些数据的集合 private List<People> mFilteredArrayList; //这个是,需要设置给adapter的 private List<People> myPeopleList; public MyListViewAdapter(Context context, List<People> list) { this.context = context; this.myforeverlist = list; //这个时候把我们的一些中间变量给初始化一下 mFilteredArrayList = new ArrayList<People>(); myPeopleList = new ArrayList<People>(); //把我们传递过来的数据,弄到这个集合里面.保存原始集合的完整性 myPeopleList.addAll(myforeverlist); } @Override public int getCount() { return myPeopleList.size(); } @Override public People getItem(int position) { return myPeopleList.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub if(convertView==null) { vHolder = new viewHolder(); convertView = View.inflate(context, R.layout.listview_cell, null); vHolder.iv_logo = (ImageView) convertView.findViewById(R.id.iv_logo); vHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name); vHolder.tv_show = (TextView) convertView.findViewById(R.id.tv_show); convertView.setTag(vHolder); } else { vHolder = (viewHolder) convertView.getTag(); } // vHolder.iv_logo.setImageResource(resId); vHolder.tv_name.setText(getItem(position).getName()); vHolder.tv_show.setText(getItem(position).getShow()); return convertView; } private viewHolder vHolder; class viewHolder{ public ImageView iv_logo; public TextView tv_name; public TextView tv_show; } private NameFilter nameFilter; public NameFilter getFilter() { if(nameFilter==null) { nameFilter = new NameFilter(); } return nameFilter; } // 过滤数据 class NameFilter extends Filter { // 执行筛选 @Override protected FilterResults performFiltering(CharSequence charSequence) { FilterResults filterResults = new FilterResults(); if (TextUtils.isEmpty(charSequence)) { mFilteredArrayList.clear();// 清除一下 filterResults.values = myforeverlist; return filterResults; } else { mFilteredArrayList.clear();// 清除一下 for (Iterator<People> iterator = myforeverlist.iterator(); iterator .hasNext();) { People name = iterator.next(); if (name.getName().contains(charSequence)) { mFilteredArrayList.add(name); } } filterResults.values = mFilteredArrayList; return filterResults; } } // 筛选结果 @Override protected void publishResults(CharSequence arg0, FilterResults results) { myPeopleList = (ArrayList<People>) results.values; if (results.count > 0) { notifyDataSetChanged(); } else { notifyDataSetInvalidated(); } } } }
activity_main.java:
package com.example.testedittextlistview;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.text.Editable;import android.text.TextWatcher;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.WindowManager;import android.view.View.OnClickListener;import android.view.inputmethod.InputMethodManager;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ArrayAdapter;import android.widget.EditText;import android.widget.ImageButton;import android.widget.ListView;import android.widget.Toast;public class MainActivity extends Activity { private EditText query; private ImageButton search_clear; private ListView lv_main; //控制输入框显示隐藏的代码 private InputMethodManager inputMethodManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { inputMethodManager= (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); query = (EditText) findViewById(R.id.query); search_clear = (ImageButton) findViewById(R.id.search_clear); search_clear.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) {//点击一下清空内容咯 query.getText().clear(); hideSoftKeyboard();//隐藏键盘 } }); //listview初始化控件并丰富一些内容 lv_main = (ListView) findViewById(R.id.lv_main); List<People> list = new ArrayList<People>(); for(int i=1;i<10;i++) { People tPeople = new People(); if(i<4) {//让内容稍微不一样点 tPeople.setName("Test"+i); tPeople.setShow("今夜不回家"+i); } else { tPeople.setName("HAHA"+i); tPeople.setShow("Love tonight"+i); } list.add(tPeople); } final MyListViewAdapter adapter = new MyListViewAdapter(getApplicationContext(), list); lv_main.setAdapter(adapter); // 为listview添加点击事件 lv_main.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // 这里注意使用的是adapter.getItem(position) // ,而不是myString[position],如果使用第二种的话,效果很坑哟 Toast.makeText(getApplicationContext(), "点击了" + adapter.getItem(position).getName(), Toast.LENGTH_SHORT) .show(); } }); // 为edittext添加内容改变的监听,onTextChanged 里面让adapter,设置过滤的条件 query.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { adapter.getFilter().filter(s); if(s.length()>0) { search_clear.setVisibility(View.VISIBLE); } else { search_clear.setVisibility(View.GONE); } } // 下面两个方法可以直接无视 @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { } }); } /** * 隐藏输入法的示例代码 */ void hideSoftKeyboard() { if (getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) { if (getCurrentFocus() != null) inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } }}
版权声明:本文为博主原创文章,未经博主允许不得转载。