欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android基于RecyclerView實(shí)現(xiàn)高亮搜索列表

 更新時(shí)間:2017年01月06日 11:53:41   作者:AndroidMsky  
本文詳細(xì)介紹了Android基于RecyclerView實(shí)現(xiàn)高亮搜索列表的方法。具有一定的參考價(jià)值,下面跟著小編一起來看下吧

話不多說先看今天的實(shí)現(xiàn)的效果:

相信這種效果很多項(xiàng)目都會(huì)用到,今天就講講利用RecycleView來實(shí)現(xiàn)他,博主把此篇文章定位初級(jí)篇,可能因?yàn)檫@確實(shí)很簡(jiǎn)單,所以我要更要講的詳細(xì)一點(diǎn)讓新手也可以能看的懂。

飯要開始做了,我們要準(zhǔn)備哪些食材呢。

1.一個(gè)RecyclerView或是listview或是其他可以顯示多item的控件(主要的干貨)

2.搞清楚EditText的實(shí)時(shí)監(jiān)聽

3.讓一個(gè)textview出現(xiàn)不同的顏色

4.如何穿過Adpter找出textview中key值(也就是高亮字符串)

當(dāng)你打通這四個(gè)技術(shù)點(diǎn)后,如果還不能實(shí)現(xiàn)這種效果,那么你能說你現(xiàn)在的學(xué)習(xí)太死板,不會(huì)活學(xué)活用。如果你看到效果立馬想到這四個(gè)技術(shù)點(diǎn)說明你有一定的項(xiàng)目組織能力了。接下來我們就解析一下這個(gè)四個(gè)食材。

1.RecyclerView猶豫這里比較簡(jiǎn)單可以使用原聲的RecyclerView,但是筆者一直在用封裝好的RecyclerView所以還用我之前封裝好的來實(shí)現(xiàn),如果對(duì)RecyclerView還不熟悉的傳送門送你走:http://blog.csdn.net/androidmsky/article/details/52922348

2.EditText事實(shí)輸入監(jiān)聽,其實(shí)就是個(gè)借口每次Editext中的字符發(fā)生改變會(huì)回調(diào)這個(gè)接口:

TextWatcher textWatcher = 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) {
 }
 };

啪啪啪一注冊(cè)接口,onTextChanged就是我們想要的方法了:

editMobile.addTextChangedListener(textWatcher);

3.textview顯示不同顏色,其實(shí)這有很多種實(shí)現(xiàn)方式,最笨的就是用兩個(gè)或者說是用三個(gè)textview左右挨著實(shí)現(xiàn)高亮顏色,然而這并不科學(xué),我們使用StringFormatUtil這樣一個(gè)工具類來實(shí)現(xiàn)高亮顏色。

StringFormatUtil spanStr3 = new 
//分別是上下文,原串,key,高亮顏色。
StringFormatUtil(mContext, data.name,
  mkey, R.color.blue).fillColor();
  customerHolder.tvName.setText(spanStr3.getResult());

4.Adapter如何知道key。

在構(gòu)造方法去傳入就可以了:

 public CustomerCampanySearchAdapter(List<Customer> list, Context context, String key) {
 super(list);
 mContext = context;
 mkey = key;
 this.list = list;
 }

好的4個(gè)食材我們就準(zhǔn)備好了

烹飪邏輯如下:

1.注冊(cè)生產(chǎn)所有類。

2.在實(shí)時(shí)監(jiān)控edittext的回調(diào)接口中重新構(gòu)造CustomerCampanySearchAdapter傳入新的key值。

public void showCustomer(List<Customer> list, String key) {
 if (list == null || list.size() == 0)
  return;
 customerList.clear();
 customerList.addAll(list);
 adapter = new CustomerCampanySearchAdapter(customerList, this, key);
 superRecyclerView.setAdapter(adapter);
 superRecyclerView.showData();
 adapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
  @Override
  public void onItemClick(View view, int position, long id) {
  //
  }
 });
 }

3.在CustomerCampanySearchAdapter中的onBindViewHolder方法中改變Textview的高亮key值。

@Override
 public void onBindViewHolder(BaseRecyclerViewHolder holder, int position, final Customer data) {
 CustomerHolder customerHolder = (CustomerHolder) holder;
 customerHolder.tvName.setText(data.name);
 StringFormatUtil spanStr3 = new StringFormatUtil(mContext, data.name,
  mkey, R.color.blue).fillColor();
 if (spanStr3 != null)
  customerHolder.tvName.setText(spanStr3.getResult());
 else customerHolder.tvName.setText(data.name);
 }

效果就這樣完美實(shí)現(xiàn)了,也提倡大家在分析的時(shí)候一定要冷靜,首先看懂自己要什么效果,之后就是你需要什么子效果,然后是怎么把這些子效果串起來實(shí)現(xiàn)最終的效果。

本文Github:歡迎star https://github.com/AndroidMsky/SearchView

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!

相關(guān)文章

最新評(píng)論