[英]CardListView filter
我正在使用來自https://github.com/gabrielemariotti/cardslib的 CardListView。 所以假設我的適配器有簡單的cardListView
CardListView cardListView = (CardListView) findViewById(R.id.card_list);
ArrayList cards = new ArrayList<>();
Card card = new Card(this);
card.setTitle("card text");
CardHeader header = new CardHeader(this);
header.setTitle("card header");
card.addCardHeader(header);
cards.add(card);
CardArrayAdapter adapter = new CardArrayAdapter(this, cards);
cardListView.setAdapter(adapter);
我想要做的是根據CardHeader過濾我的cardListView。 CardArrayAdapter具有該方法
adapter.getFilter().filter("some text")
但我不明白它如何過濾卡片。 就我而言,我把它放進去了
@Override
public boolean onQueryTextChange(String s) {
adapter.getFilter().filter(s);
return true;
}
但它沒有在我的列表中找到任何卡片,也沒有找到我在card.setTitle()中設置的文本,也沒有找到header.setTitle() 。
有誰知道它是如何工作的? 我非常感謝您花時間分享您的想法。
正如我在源代碼中看到的那樣,庫沒有自定義過濾器的實現,因此首先必須實現類似於此的自定義過濾器:
Filter cardFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
ArrayList<Card> tempList = new ArrayList<Card>();
// Where constraint is the value you're filtering against and
// cards is the original list of elements
if(constraint != null && cards!=null) {
// Iterate over the cards list and add the wanted
// items to the tempList
filterResults.values = tempList;
filterResults.count = tempList.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
// Update your adapter here and notify
cardArrayAdapter.addAll(results.values);
cardArrayAdapter.notifyDataSetChanged();
}
};
之后,就我所見,您有2個選項:
1)修改庫的源代碼,並重寫getFilter()
的方法中CardArrayAdapter
或BaseCardArrayAdapter
返回您的實例customFilter
2)直接在代碼中實現過濾邏輯,只有在更新onQueryTextChanged
的文本時才更新適配器
在這里你可以找到代碼的參考: 在android中自定義ArrayAdapter中的自定義getFilter
arrayAdapter實現了Filterable。 例如,它適用於Strings或int。 在您的情況下,您正在使用卡。 在我看來,首先你應該覆蓋卡中的toString()方法。
ArrayAdapter中的默認getFilter()方法使用object.toString()來過濾列表。
如果沒有足夠的實施自定義過濾器。
此致,哈維爾。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.