内容简介:Android EditText达到SearchView的效果
先来看下效果图:
就不啰嗦了,直接上代码:
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<String> mDatas; private List<String> 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<String>{ 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布局:
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://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,写得还是很不错的,通俗易懂,这里强烈推荐各位去学习下: http://blog.csdn.net/lmj623565791/article/details/38902805
可以跟着敲一遍,我这里就直接贴出来了,就两个类:
ViewHolder:
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<View> 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 <T> * @return */ public <T extends View> 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:
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. */ 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); }
这里其实可以封装成一个通用的控件,有兴趣的话可以参考下这篇博客: http://blog.csdn.net/Yankee1967/article/details/52291487?locationNum=3&fps=1
以上所述就是小编给大家介绍的《Android EditText达到SearchView的效果》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- Android EditText达到SearchView的效果
- 深度学习已经达到极限了吗?
- CITA 是如何达到 15000 TPS 的?
- 如何修剪BERT达到加速目的?理论与实现
- USB4 规范发布,双倍带宽,达到 40 Gbps
- oracle 表空间文件达到32G后手动添加数据文件
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。