先来看下效果图:
就不啰嗦了,直接上代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.example.bao.mysearchview;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private EditText editText;
private ListView listView;
private List mDatas;
private List mCopyDatas;
private MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText= (EditText) findViewById(R.id.etSearch);
listView= (ListView) findViewById(R.id.list_view);
mDatas= new ArrayList<>();
mCopyDatas= new ArrayList<>();
setDatas();
listView.setAdapter(myAdapter= new MyAdapter( this , mDatas));
editText.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) {
String searchText = s.toString();
mDatas.clear();
if (TextUtils.isEmpty(searchText)){
mDatas.addAll(mCopyDatas);
} else {
for (String str : mCopyDatas) {
if (str.contains(searchText)){
mDatas.add(str);
}
}
}
myAdapter.notifyDataSetChanged();
}
});
}
private void setDatas() {
for ( int i = 0 ; i < 5 ; i++) {
mCopyDatas.add( "11" +i);
}
for ( int i = 0 ; i < 5 ; i++) {
mCopyDatas.add( "22" +i);
}
for ( int i = 0 ; i < 5 ; i++) {
mCopyDatas.add( "33" +i);
}
for ( int i = 0 ; i < 5 ; i++) {
mCopyDatas.add( "12" +i);
}
for ( int i = 0 ; i < 5 ; i++) {
mCopyDatas.add( "13" +i);
}
mDatas.addAll(mCopyDatas);
}
private class MyAdapter extends CommonAdapter{
public MyAdapter(Context context, List data) {
super (context, data, R.layout.item);
}
@Override
protected void convert(ViewHolder viewHolder, String item) {
viewHolder.setText(R.id.id_tv_title,item);
}
}
}
item布局:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<? xml version = "1.0" encoding = "utf-8" ?>
< TextView xmlns:android = "https://schemas.android.com/apk/res/android"
android:id = "@+id/id_tv_title"
android:layout_width = "match_parent"
android:layout_height = "50dp"
android:background = "#aa111111"
android:gravity = "center_vertical"
android:paddingLeft = "15dp"
android:textColor = "#ffffff"
android:text = "hello"
android:textSize = "20sp"
android:textStyle = "bold" >
</ TextView >
ps:这里使用了张鸿洋的一个CommonAdapter,写得还是很不错的,通俗易懂,这里强烈推荐各位去学习下:https://blog.csdn.net/lmj623565791/article/details/38902805
可以跟着敲一遍,我这里就直接贴出来了,就两个类:
ViewHolder:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.example.bao.mysearchview;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
/**
* Created by bao on 2017/5/31.
*/
public class ViewHolder {
private View mConvertView;
private SparseArray mViews;
private int mPosition;
private ViewHolder(Context context, ViewGroup parent, int layoutId, int position) {
mViews= new SparseArray<>();
mConvertView = LayoutInflater.from(context).inflate(layoutId,parent, false );
mConvertView.setTag( this );
mPosition=position;
}
/**
* 拿到一个Viewholder对象
* @param context
* @param convertView
* @param parent
* @param layoutId
* @return
*/
public static ViewHolder get(Context context,View convertView,ViewGroup parent, int layoutId, int position){
if (convertView== null ){
return new ViewHolder(context,parent,layoutId,position);
}
return (ViewHolder) convertView.getTag();
}
/**
* 通过ID获取控件对象,如果没有则加入到views
* @param viewId
* @param
* @return
*/
public T getView( int viewId){
View view = mViews.get(viewId);
if (view== null ){
view= mConvertView.findViewById(viewId);
mViews.put(viewId,view);
}
return (T) view;
}
public View getConvertView(){
return mConvertView;
}
/**
* 为textview设置字符串
* @param viewId
* @param text
* @return
*/
public ViewHolder setText( int viewId,String text){
TextView textView=getView(viewId);
textView.setText(text);
return this ;
}
/**
* 为ImageView设置图片
* @param viewId
* @param drawableId
* @return
*/
public ViewHolder setImageResource( int viewId, int drawableId){
ImageView view = getView(viewId);
view.setImageResource(drawableId);
return this ;
}
/**
* 为ImageView设置图片
*
* @param viewId
* @param bm
* @return
*/
public ViewHolder setImageBitmap( int viewId, Bitmap bm)
{
ImageView view = getView(viewId);
view.setImageBitmap(bm);
return this ;
}
/**
* 可以按照自己的需求继续添加类似setText、setImageResource、setImageBitmap等方法哦
*
*/
public int getItemPosition(){
return mPosition;
}
}
CommonAdapter:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.example.bao.mysearchview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.List;
/**
* Created by bao on 2017/5/31.
*/
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.List;
public abstract class CommonAdapter<T> extends BaseAdapter {
protected Context mContext;
protected List<T> mDatas;
protected LayoutInflater mInflater;
protected int mLayoutId;
public CommonAdapter(Context context, List<T> data, int layoutId) {
mInflater=LayoutInflater.from(context);
this .mContext = context;
this .mDatas = data;
mLayoutId=layoutId;
}
@Override
public int getCount() {
return mDatas.size();
}
@Override
public T getItem( int position) {
return mDatas.get(position);
}
@Override
public long getItemId( int position) {
return position;
}
@Override
public View getView( int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = ViewHolder.get(mContext, convertView, parent, mLayoutId,position);
convert(viewHolder,getItem(position));
return viewHolder.getConvertView();
}
protected abstract void convert(ViewHolder viewHolder, T item);
}
这里其实可以封装成一个通用的控件,有兴趣的话可以参考下这篇博客:https://blog.csdn.net/Yankee1967/article/details/52291487?locationNum=3&fps=1
项目源码:https://download.csdn.net/detail/qq_33748378/9856610
标签: 60秒安卓手机中文版下载