Android搜索框通用版
之前项目总会遇到很多搜索框类的功能,虽然不是很复杂,不过每次都要去自己处理数据,并且去处理搜索框的变化,写起来也比较麻烦,今天来做一个比较简单的通用搜索栏。
先看下效果图:
没什么特别的,只是今天要做的就是简单的把搜索框的内容封装一下.
一、分析功能
先考虑一下,搜索框一般都是由一个搜索图标(一般都是一个放大镜),一个输入框和一个清除按钮组成.然后会通过监听输入框的变化去处理清除按钮的显示和隐藏并且去过滤相关的数据.最后去刷新适配器,显示过滤后的数据.基本上搜索框的功能都大同小异.
有了上边的分析,我们可以去提取出一个通用的搜索框布局,然后写一个自定义viewgroup去处理相关的页面显示等问题.
二、具体实现
先来写一些自定的属性,方便之后使用,新建一个attr文件并定义自定义属性如下:
<?xml version="1.0" encoding="utf-8"?> <resources> <!--搜索图标左边margin--> <attr name="searchbariconmarginleft" format="dimension"/> <!--搜索图标右边margin--> <attr name="searchbariconmarginright" format="dimension"/> <!--搜索图标--> <attr name="searchbariconsrc" format="reference"/> <!--清除图标左边margin--> <attr name="cleariconmarginleft" format="dimension"/> <!--清除图标右边margin--> <attr name="cleariconmarginright" format="dimension"/> <!--清除图标--> <attr name="cleariconsrc" format="reference"/> <!--搜索文字大小--> <attr name="searchtextsize" format="dimension"/> <!--搜索文字颜色--> <attr name="searchtextcolor" format="color"/> <declare-styleable name="commolysearchview"> <attr name="searchbariconmarginleft"/> <attr name="searchbariconmarginright"/> <attr name="searchbariconsrc"/> <attr name="cleariconmarginleft"/> <attr name="cleariconmarginright"/> <attr name="cleariconsrc"/> <attr name="searchtextsize"/> <attr name="searchtextcolor"/> </declare-styleable> </resources>
然后写一下搜索栏的布局searchview_layout.xml:
<?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="vertical"> <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical" android:orientation="horizontal" > <imageview android:id="@+id/iv_search_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:src="@mipmap/search_bar_icon" android:layout_marginright="8dp" /> <edittext android:id="@+id/et_search" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@null" android:singleline="true" /> <imageview android:id="@+id/iv_search_clear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:src="@mipmap/search_clear_icon" android:visibility="gone" /> </linearlayout> </linearlayout>
布局很简单,就是一个搜索图标加上一个输入框和一个清除图标,下边来写一下具体的实现,建立一个commolysearchview继承自linearlayout即可,先来定义一些属性,获取自定义属性并且绑定布局文件.代码如下:
/** * created by junweiliu on 16/5/31. */ public class commolysearchview<t> extends linearlayout { /** * 上下文 */ private context mcontext; /** * 编辑框 */ private edittext medittext; /** * 清除按钮 */ private imageview mclearimg; /** * 搜索图标 */ private imageview msearchbarimg; /** * 适配器 */ private baseadapter madapter; /** * 数据源 */ private list<t> mdatas = new arraylist<t>(); /** * 数据源副本 */ private list<t> mdupdatas = new arraylist<t>(); /** * 筛选后的数据源 */ private list<t> mfilterdatas = new arraylist<t>(); /** * 筛选后的数据源副本 */ private list<t> mdupfilterdatas = new arraylist<t>(); /** * 搜索图标 */ private bitmap msearchicon; /** * 搜索框距离左边边距 */ private int msearchiconmarginleft; /** * 搜索框距离右边边距 */ private int msearchiconmarginright; /** * 清除图标 */ private bitmap mclearicon; /** * 清除图标距离左边边距 */ private int mcleariconmarginleft; /** * 清除图标距离右边边距 */ private int mcleariconmarginright; /** * 搜索文字大小 */ private int msearchtextsize; /** * 搜索文字颜色 */ private int msearchtextcolor; public commolysearchview(context context) { this(context, null); } public commolysearchview(context context, attributeset attrs) { this(context, attrs, 0); } public commolysearchview(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); mcontext = context; // 自定义属性 typedarray ta = context.obtainstyledattributes(attrs, r.styleable.commolysearchview); drawable searchd = ta.getdrawable(r.styleable.commolysearchview_searchbariconsrc); msearchicon = drawabletobitamp(searchd); msearchiconmarginleft = px2dip(context, ta.getdimensionpixeloffset(r.styleable.commolysearchview_searchbariconmarginleft, 0)); msearchiconmarginright = px2dip(context, ta.getdimensionpixeloffset(r.styleable.commolysearchview_searchbariconmarginright, 0)); drawable cleard = ta.getdrawable(r.styleable.commolysearchview_cleariconsrc); mclearicon = drawabletobitamp(cleard); mcleariconmarginleft = px2dip(context, ta.getdimensionpixeloffset(r.styleable.commolysearchview_cleariconmarginleft, 0)); mcleariconmarginright = px2dip(context, ta.getdimensionpixeloffset(r.styleable.commolysearchview_cleariconmarginright, 0)); msearchtextsize = px2sp(context, ta.getdimensionpixeloffset(r.styleable.commolysearchview_searchtextsize, 0)); msearchtextcolor = ta.getcolor(r.styleable.commolysearchview_searchtextcolor, 0); ta.recycle(); // 绑定布局文件 layoutinflater.from(context).inflate(r.layout.searchview_layout, this); initview(); }
可以看到我们使用了泛型,目的是为了处理不同的数据源类型,接下来需要初始化控件,看一下initview方法:
/** * 初始化控件 */ private void initview() { medittext = (edittext) findviewbyid(r.id.et_search); mclearimg = (imageview) findviewbyid(r.id.iv_search_clear); msearchbarimg = (imageview) findviewbyid(r.id.iv_search_icon); // 处理自定义属性 if (0 != msearchiconmarginleft || 0 != msearchiconmarginright) { msearchbarimg.setpadding(msearchiconmarginleft, 0, msearchiconmarginright, 0); } if (0 != mcleariconmarginleft || 0 != mcleariconmarginright) { mclearimg.setpadding(mcleariconmarginleft, 0, mcleariconmarginright, 0); } if (null != msearchicon) { msearchbarimg.setimagebitmap(msearchicon); } if (null != mclearicon) { mclearimg.setimagebitmap(mclearicon); } if (0 != msearchtextsize) { medittext.settextsize(msearchtextsize); } if (0 != msearchtextcolor) { medittext.settextcolor(msearchtextcolor); } // 清空按钮处理事件 mclearimg.setonclicklistener(new onclicklistener() { @override public void onclick(view view) { } }); // 搜索栏处理事件 medittext.addtextchangedlistener(new textwatcher() { @override public void beforetextchanged(charsequence charsequence, int i, int i1, int i2) { } @override public void ontextchanged(charsequence charsequence, int i, int i1, int i2) { } @override public void aftertextchanged(editable editable) { } }); }
这样初始化控件的任务也完成了,下面就是处理具体逻辑的部分,我们打算在commonlysearchview完成清除按钮的显示隐藏,编辑框内容的处理,对适配器进行刷新的功能,具体数据的筛选工作,使用一个回调接口,由使用者去进行筛选,只需要获取筛选后的数据即可,然后通过筛选后的数据,去刷新适配器来完成要实现的功能.所以要对外提供一些方法,来获取到适配器,及一些数据源.
/** * 设置数据源 * * @param datas */ public void setdatas(list<t> datas) { if (null == datas) { return; } if (null != mdatas) { mdatas.clear(); } if (null != mdupdatas) { mdupdatas.clear(); } mdatas = datas; mdupdatas.addall(mdatas); } /** * 设置适配器 * * @param adapter */ public void setadapter(baseadapter adapter) { if (null == adapter) { return; } madapter = adapter; }
获取到适配器及数据源,并且做了相关数据备份工作.
接着要提供一个回调接口来得到筛选后的数据.
/** * 回调接口 * * @param <t> */ interface searchdatas<t> { /** * 参数一:全部数据,参数二:筛选后的数据,参数三:输入的内容 * * @param datas * @param filterdatas * @param inputstr * @return 筛选后的数据 */ list<t> filterdatas(list<t> datas, list<t> filterdatas, string inputstr); } /** * 回调 */ private searchdatas<t> mlistener; /** * 设置回调 * * @param listener */ public void setsearchdatalistener(searchdatas<t> listener) { mlistener = listener; }
同样需要对外提供一个方法,以便使用者获取到筛选后的数据去做相关操作.
/** * 获取筛选后的数据 * * @return */ public list<t> getfilterdatas() { return (null != mdupfilterdatas && mdupfilterdatas.size() > 0) ? mdupfilterdatas : mdupdatas; }
得到数据之后,就是在输入框和清除按钮的监听方法中去做相关处理就可以了,完整代码如下:
package com.example.junweiliu.commonlysearchview; import android.content.context; import android.content.res.typedarray; import android.graphics.bitmap; import android.graphics.canvas; import android.graphics.drawable.bitmapdrawable; import android.graphics.drawable.drawable; import android.text.editable; import android.text.textwatcher; import android.util.attributeset; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.widget.baseadapter; import android.widget.button; import android.widget.edittext; import android.widget.imageview; import android.widget.linearlayout; import android.widget.listview; import java.util.arraylist; import java.util.list; /** * created by junweiliu on 16/5/31. */ public class commolysearchview<t> extends linearlayout { /** * 上下文 */ private context mcontext; /** * 编辑框 */ private edittext medittext; /** * 清除按钮 */ private imageview mclearimg; /** * 搜索图标 */ private imageview msearchbarimg; /** * 适配器 */ private baseadapter madapter; /** * 数据源 */ private list<t> mdatas = new arraylist<t>(); /** * 数据源副本 */ private list<t> mdupdatas = new arraylist<t>(); /** * 筛选后的数据源 */ private list<t> mfilterdatas = new arraylist<t>(); /** * 筛选后的数据源副本 */ private list<t> mdupfilterdatas = new arraylist<t>(); /** * 搜索图标 */ private bitmap msearchicon; /** * 搜索框距离左边边距 */ private int msearchiconmarginleft; /** * 搜索框距离右边边距 */ private int msearchiconmarginright; /** * 清除图标 */ private bitmap mclearicon; /** * 清除图标距离左边边距 */ private int mcleariconmarginleft; /** * 清除图标距离右边边距 */ private int mcleariconmarginright; /** * 搜索文字大小 */ private int msearchtextsize; /** * 搜索文字颜色 */ private int msearchtextcolor; /** * 回调接口 * * @param <t> */ interface searchdatas<t> { /** * 参数一:全部数据,参数二:筛选后的数据,参数三:输入的内容 * * @param datas * @param filterdatas * @param inputstr * @return */ list<t> filterdatas(list<t> datas, list<t> filterdatas, string inputstr); } /** * 回调 */ private searchdatas<t> mlistener; /** * 设置回调 * * @param listener */ public void setsearchdatalistener(searchdatas<t> listener) { mlistener = listener; } public commolysearchview(context context) { this(context, null); } public commolysearchview(context context, attributeset attrs) { this(context, attrs, 0); } public commolysearchview(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); mcontext = context; // 自定义属性 typedarray ta = context.obtainstyledattributes(attrs, r.styleable.commolysearchview); drawable searchd = ta.getdrawable(r.styleable.commolysearchview_searchbariconsrc); msearchicon = drawabletobitamp(searchd); msearchiconmarginleft = px2dip(context, ta.getdimensionpixeloffset(r.styleable.commolysearchview_searchbariconmarginleft, 0)); msearchiconmarginright = px2dip(context, ta.getdimensionpixeloffset(r.styleable.commolysearchview_searchbariconmarginright, 0)); drawable cleard = ta.getdrawable(r.styleable.commolysearchview_cleariconsrc); mclearicon = drawabletobitamp(cleard); mcleariconmarginleft = px2dip(context, ta.getdimensionpixeloffset(r.styleable.commolysearchview_cleariconmarginleft, 0)); mcleariconmarginright = px2dip(context, ta.getdimensionpixeloffset(r.styleable.commolysearchview_cleariconmarginright, 0)); msearchtextsize = px2sp(context, ta.getdimensionpixeloffset(r.styleable.commolysearchview_searchtextsize, 0)); msearchtextcolor = ta.getcolor(r.styleable.commolysearchview_searchtextcolor, 0); ta.recycle(); // 绑定布局文件 layoutinflater.from(context).inflate(r.layout.searchview_layout, this); initview(); } /** * 初始化控件 */ private void initview() { medittext = (edittext) findviewbyid(r.id.et_search); mclearimg = (imageview) findviewbyid(r.id.iv_search_clear); msearchbarimg = (imageview) findviewbyid(r.id.iv_search_icon); // 处理自定义属性 if (0 != msearchiconmarginleft || 0 != msearchiconmarginright) { msearchbarimg.setpadding(msearchiconmarginleft, 0, msearchiconmarginright, 0); } if (0 != mcleariconmarginleft || 0 != mcleariconmarginright) { mclearimg.setpadding(mcleariconmarginleft, 0, mcleariconmarginright, 0); } if (null != msearchicon) { msearchbarimg.setimagebitmap(msearchicon); } if (null != mclearicon) { mclearimg.setimagebitmap(mclearicon); } if (0 != msearchtextsize) { medittext.settextsize(msearchtextsize); } if (0 != msearchtextcolor) { medittext.settextcolor(msearchtextcolor); } // 清空按钮处理事件 mclearimg.setonclicklistener(new onclicklistener() { @override public void onclick(view view) { medittext.settext(""); mclearimg.setvisibility(view.gone); if (null != mdatas) { mdatas.clear(); } mdatas.addall(mdupdatas); madapter.notifydatasetchanged(); resetdatas(); } }); // 搜索栏处理事件 medittext.addtextchangedlistener(new textwatcher() { @override public void beforetextchanged(charsequence charsequence, int i, int i1, int i2) { } @override public void ontextchanged(charsequence charsequence, int i, int i1, int i2) { // 获取筛选后的数据 mfilterdatas = mlistener.filterdatas(mdupdatas, mfilterdatas, charsequence.tostring()); if (charsequence.tostring().length() > 0 && !charsequence.tostring().equals("")) { mclearimg.setvisibility(view.visible); } else { mclearimg.setvisibility(view.gone); } if (null != mdatas) { mdatas.clear(); } mdatas.addall(mfilterdatas); madapter.notifydatasetchanged(); resetdatas(); } @override public void aftertextchanged(editable editable) { } }); } /** * 获取筛选后的数据 * * @return */ public list<t> getfilterdatas() { return (null != mdupfilterdatas && mdupfilterdatas.size() > 0) ? mdupfilterdatas : mdupdatas; } /** * 重置数据 */ private void resetdatas() { if (null != mfilterdatas) { if (null != mdupfilterdatas) { mdupfilterdatas.clear(); mdupfilterdatas.addall(mfilterdatas); } mfilterdatas.clear(); } } /** * 设置数据源 * * @param datas */ public void setdatas(list<t> datas) { if (null == datas) { return; } if (null != mdatas) { mdatas.clear(); } if (null != mdupdatas) { mdupdatas.clear(); } mdatas = datas; mdupdatas.addall(mdatas); } /** * 设置适配器 * * @param adapter */ public void setadapter(baseadapter adapter) { if (null == adapter) { return; } madapter = adapter; } /** * drawable转bitmap * * @param drawable * @return */ private bitmap drawabletobitamp(drawable drawable) { if (null == drawable) { return null; } if (drawable instanceof bitmapdrawable) { bitmapdrawable bd = (bitmapdrawable) drawable; return bd.getbitmap(); } int w = drawable.getintrinsicwidth(); int h = drawable.getintrinsicheight(); bitmap bitmap = bitmap.createbitmap(w, h, bitmap.config.argb_8888); canvas canvas = new canvas(bitmap); drawable.setbounds(0, 0, w, h); drawable.draw(canvas); return bitmap; } /** * 将px值转换为dip或dp值,保证尺寸大小不变 * * @param pxvalue * @return * @param(displaymetrics类中属性density) */ public int px2dip(context context, float pxvalue) { final float scale = context.getresources().getdisplaymetrics().density; return (int) (pxvalue / scale + 0.5f); } /** * 将px值转换为sp值,保证文字大小不变 * * @param pxvalue * @return */ public static int px2sp(context context, float pxvalue) { final float fontscale = context.getresources().getdisplaymetrics().scaleddensity; return (int) (pxvalue / fontscale + 0.5f); } }
三、完整代码及使用
换一个复杂点的listview来使用一下.很简单,就是普通的listview绑定数据,我们结合commolysearchview来使用一下.
实体bean
sanguobean:
package com.example.junweiliu.commonlysearchview.bean; /** * created by junweiliu on 16/6/1. */ public class sanguobean { /** * 名称 */ private string sgname; /** * 描述 */ private string sgdescribe; /** * 头像 */ private int sgheadbp; /** * 字 */ private string sgpetname; public sanguobean() { } public sanguobean(string sgname, string sgdescribe, int sgheadbp, string sgpetname) { this.sgname = sgname; this.sgdescribe = sgdescribe; this.sgheadbp = sgheadbp; this.sgpetname = sgpetname; } public string getsgname() { return sgname; } public void setsgname(string sgname) { this.sgname = sgname; } public string getsgdescribe() { return sgdescribe; } public void setsgdescribe(string sgdescribe) { this.sgdescribe = sgdescribe; } public int getsgheadbp() { return sgheadbp; } public void setsgheadbp(int sgheadbp) { this.sgheadbp = sgheadbp; } public string getsgpetname() { return sgpetname; } public void setsgpetname(string sgpetname) { this.sgpetname = sgpetname; } }
适配器每项布局文件sg_item:
<?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:gravity="center_vertical" android:orientation="horizontal" android:padding="6dp" > <imageview android:id="@+id/iv_sghead" android:layout_width="80dp" android:layout_height="80dp" android:scaletype="fitxy" android:src="@mipmap/ic_launcher" /> <linearlayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginleft="8dp" android:layout_weight="1" android:orientation="vertical" > <textview android:id="@+id/tv_sgname" android:layout_width="match_parent" android:layout_height="wrap_content" android:textsize="16sp" /> <textview android:id="@+id/tv_sgpetname" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="2dp" android:textsize="12sp" /> <textview android:id="@+id/tv_sgdes" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="2dp" android:textsize="12sp" /> </linearlayout> </linearlayout>
适配器sgadapter:
package com.example.junweiliu.commonlysearchview.adapter; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.imageview; import android.widget.textview; import com.example.junweiliu.commonlysearchview.r; import com.example.junweiliu.commonlysearchview.bean.sanguobean; import com.example.junweiliu.commonlysearchview.bean.searchbean; import java.util.list; /** * created by junweiliu on 16/6/1. */ public class sgadapter extends baseadapter { /** * 上下文 */ private context mcontext; /** * 数据源 */ private list<sanguobean> mdatas; /** * 构造函数 * * @param context * @param datas */ public sgadapter(context context, list<sanguobean> datas) { mcontext = context; mdatas = datas; } @override public int getcount() { return mdatas.size(); } @override public object getitem(int i) { return mdatas.get(i); } @override public long getitemid(int i) { return i; } @override public view getview(int position, view view, viewgroup viewgroup) { viewholder vh = null; if (null == view) { vh = new viewholder(); layoutinflater minflater = layoutinflater.from(mcontext); view = minflater.inflate(r.layout.sg_item, null); vh.mnametv = (textview) view.findviewbyid(r.id.tv_sgname); vh.mpetnmaetv = (textview) view.findviewbyid(r.id.tv_sgpetname); vh.mdestv = (textview) view.findviewbyid(r.id.tv_sgdes); vh.mheadimg = (imageview) view.findviewbyid(r.id.iv_sghead); view.settag(vh); } else { vh = (viewholder) view.gettag(); } sanguobean bean = (sanguobean) getitem(position); if (null != bean) { vh.mnametv.settext(bean.getsgname()); vh.mdestv.settext(bean.getsgdescribe()); vh.mpetnmaetv.settext(bean.getsgpetname()); vh.mheadimg.setimageresource(bean.getsgheadbp()); } return view; } /** * vh */ class viewholder { /** * 姓名 */ textview mnametv; /** * 描述 */ textview mdestv; /** * 字 */ textview mpetnmaetv; /** * 头像 */ imageview mheadimg; } }
mainactivity布局文件activity_main:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:search="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.junweiliu.commonlysearchview.mainactivity"> <com.example.junweiliu.commonlysearchview.commolysearchview android:id="@+id/csv_show" android:layout_width="match_parent" android:layout_height="40dp" android:layout_margin="8dip" android:background="@drawable/login_et_bg" search:searchtextcolor="#ff0000" search:searchtextsize="14sp" > </com.example.junweiliu.commonlysearchview.commolysearchview> <listview android:id="@+id/lv_show" android:layout_width="match_parent" android:layout_height="match_parent" > </listview> </linearlayout>
mainactivity:
package com.example.junweiliu.commonlysearchview; import android.app.activity; import android.os.bundle; import android.util.log; import android.view.view; import android.widget.adapterview; import android.widget.listview; import android.widget.toast; import com.example.junweiliu.commonlysearchview.adapter.sgadapter; import com.example.junweiliu.commonlysearchview.adapter.searchadapter; import com.example.junweiliu.commonlysearchview.bean.sanguobean; import com.example.junweiliu.commonlysearchview.bean.searchbean; import java.util.arraylist; import java.util.list; public class mainactivity extends activity { /** * tag */ private static final string tag = "mainactivity"; /** * 数据显示listview */ private listview mlistview; /** * 适配器 */ private searchadapter adapter; /** * 三国通用搜索框 */ private commolysearchview<sanguobean> msgcommolysearchview; /** * 三国数据源 */ private list<sanguobean> msgdatas; /** * 三国适配器 */ private sgadapter sgadapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initdatai(); initviewi(); } /** * 初始化数据 */ private void initdatai() { msgdatas = new arraylist<sanguobean>(); sanguobean sgbean1 = new sanguobean(); sgbean1.setsgname("刘备"); sgbean1.setsgpetname("玄德"); sgbean1.setsgheadbp(r.drawable.lb); sgbean1.setsgdescribe("刘备(161年-223年6月10日),字玄德,东汉末年幽州涿郡涿县(今河北省涿州市)人"); sanguobean sgbean2 = new sanguobean(); sgbean2.setsgname("关羽"); sgbean2.setsgpetname("云长"); sgbean2.setsgheadbp(r.drawable.gy); sgbean2.setsgdescribe("关羽(?-220年),本字长生,后改字云长,河东郡解良(今山西运城)人"); sanguobean sgbean3 = new sanguobean(); sgbean3.setsgname("张飞"); sgbean3.setsgpetname("翼德"); sgbean3.setsgheadbp(r.drawable.zf); sgbean3.setsgdescribe("张飞(?-221年),字益德[1] ,幽州涿郡(今河北省保定市涿州市)人氏"); sanguobean sgbean4 = new sanguobean(); sgbean4.setsgname("赵云"); sgbean4.setsgpetname("子龙"); sgbean4.setsgheadbp(r.drawable.zy); sgbean4.setsgdescribe("赵云(?-229年),字子龙,常山真定(今河北省正定)人"); sanguobean sgbean5 = new sanguobean(); sgbean5.setsgname("马超"); sgbean5.setsgpetname("孟起"); sgbean5.setsgheadbp(r.drawable.mc); sgbean5.setsgdescribe("马超(176年-222年),字孟起,司隶部扶风郡茂陵(今陕西兴平)人"); sanguobean sgbean6 = new sanguobean(); sgbean6.setsgname("黄忠"); sgbean6.setsgpetname("汉升"); sgbean6.setsgheadbp(r.drawable.hz); sgbean6.setsgdescribe("黄忠(?-220年),字汉升(一作“汉叔”[1] ),南阳(今河南南阳)人"); sanguobean sgbean7 = new sanguobean(); sgbean7.setsgname("张辽"); sgbean7.setsgpetname("文远"); sgbean7.setsgheadbp(r.drawable.zl); sgbean7.setsgdescribe("张辽(169年-222年),字文远,雁门马邑(今山西朔州)人"); msgdatas.add(sgbean1); msgdatas.add(sgbean2); msgdatas.add(sgbean3); msgdatas.add(sgbean4); msgdatas.add(sgbean5); msgdatas.add(sgbean6); msgdatas.add(sgbean7); } /** * 初始化控件 */ private void initviewi() { msgcommolysearchview = (commolysearchview) findviewbyid(r.id.csv_show); mlistview = (listview) findviewbyid(r.id.lv_show); sgadapter = new sgadapter(this, msgdatas); mlistview.setadapter(sgadapter); // 设置数据源 msgcommolysearchview.setdatas(msgdatas); // 设置适配器 msgcommolysearchview.setadapter(sgadapter); // 设置筛选数据 msgcommolysearchview.setsearchdatalistener(new commolysearchview.searchdatas<sanguobean>() { @override public list<sanguobean> filterdatas(list<sanguobean> datas, list<sanguobean> filterdatas, string inputstr) { for (int i = 0; i < datas.size(); i++) { // 筛选条件 if ((datas.get(i).getsgdescribe()).contains(inputstr) || datas.get(i).getsgname().contains(inputstr) || datas.get(i).getsgpetname().contains(inputstr)) { filterdatas.add(datas.get(i)); } } return filterdatas; } }); mlistview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> adapterview, view view, int i, long l) { toast.maketext(mainactivity.this, msgcommolysearchview.getfilterdatas().get(i).getsgname() + "字" + msgcommolysearchview.getfilterdatas().get(i).getsgpetname() + "\n" + msgcommolysearchview.getfilterdatas().get(i).getsgdescribe(), toast.length_short).show(); } }); } }
效果图如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。