我想搜索RecyclerView
,我有List<BaseOfCards>
(BaseOfCards是我的getter&amp; setter类)
我的 RecyclerViewAdapter :
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
private LayoutInflater inflater;
private List<BaseOfCards> items;
//private int itemLayout;
//String cardvalue;
private Activity mActivity;
public RecyclerViewAdapter(Activity mActivity, Context context, List<BaseOfCards> items) {
this.mActivity = mActivity;
inflater = LayoutInflater.from(context);
this.items = items;
//this.itemLayout = itemLayout;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.custom_row, parent, false);
MyViewHolder holder = new MyViewHolder(view, mActivity);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
BaseOfCards item = items.get(position);
holder.title.setTag(item);
holder.title.setText(item.getCardName());
}
@Override
public int getItemCount() {
return items.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
private Activity mActivity;
TextView title;
ImageView titileImageView;
public MyViewHolder(View itemView, Activity mActivity) {
super(itemView);
titileImageView = (ImageView) itemView.findViewById(R.id.image_country);
title = (TextView) itemView.findViewById(R.id.listText);
this.mActivity = mActivity;
}
}
}
我将SearchView添加到我的菜单并在 MainActivity 中初始化:
MenuItem menuItem = menu.findItem(R.id.action_search1);
searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setIconifiedByDefault(true);
接下来我需要做什么?让我的 RecyclerViewAdapter implement Filterable
或者什么?或者只是在* RecyclerViewAdapter **中创建课程filter
,然后从我的 MainActivity 中调用它?
答案 0 :(得分:19)
我解决了我的问题
让我的课 RecyclerViewAdapter implements Filterable
添加行private List<BaseOfCards> orig;
在 RecyclerViewAdapter
中添加方法getFilter
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults oReturn = new FilterResults();
final List<BaseOfCards> results = new ArrayList<BaseOfCards>();
if (orig == null)
orig = items;
if (constraint != null){
if(orig !=null & orig.size()>0 ){
for ( final BaseOfCards g :orig) {
if (g.getCardName().toLowerCase().contains(constraint.toString()))results.add(g);
}
}
oReturn.values = results;
}
return oReturn;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
items = (ArrayList<BaseOfCards>)results.values;
notifyDataSetChanged();
}
};
制作 MainActivity implements SearchView.OnQueryTextListener
并更改方法onQueryTextChange
:
@Override
public boolean onQueryTextChange(String newText) {
if ( TextUtils.isEmpty ( newText ) ) {
adapter.getFilter().filter("");
} else {
adapter.getFilter().filter(newText.toString());
}
return true;
}
答案 1 :(得分:0)
使用autocompletetextview或edittext我处理了这个
public List<SalesProductsItems> mItems
是最初的listitem实例和。
public static List<SalesProductsItems> filteredIt
是用于显示项目的实例。自从第一次过滤结果不为空时,mItems
实例将等于filteredIt
实例(从而丢失初始列表),然后在{在publishResults
失去原始值之前的{1}}方法,我将其等同于传递的实例mItems
。希望它可以帮助某人
originallist
答案 2 :(得分:0)
我想添加ololoking回答。
在MainActivity
中,我们还应该添加下一个代码,以便它可以正常工作:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_layout, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setIconifiedByDefault(true);
searchView.setOnQueryTextListener(this);
return super.onCreateOptionsMenu(menu);
}
感谢你的回答。它帮助了我。
答案 3 :(得分:0)
从另一个肯定答案完成之时起,我现在在FlexibleAdapter库中使用AsyncTask
实施了快速异步过滤器,性能非常好有大名单,也有动画!
适配器可配置为在过滤结果中启用/禁用属性以在必要时提高速度。另一个很大的优点是界面仍然响应用户。
在运行Android 6的Samsung S3中进行测试:开始列表中包含10.450项,从后台进程启动时起,需要约1秒来过滤字符并选择3.890项。
我还完成了一个Wiki page,其中包含了使用适配器过滤器的所有细节。
答案 4 :(得分:0)
在您的适配器类中扩展可过滤的。 然后覆盖公共Filter getFilter()。
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
FilterResults filterResults = new FilterResults();
if(charSequence == null | charSequence.length() == 0){
filterResults.count = getUserModelListFiltered.size();
filterResults.values = getUserModelListFiltered;
}else{
String searchChr = charSequence.toString().toLowerCase();
List<UserModel> resultData = new ArrayList<>();
for(UserModel userModel: getUserModelListFiltered){
if(userModel.getUserName().toLowerCase().contains(searchChr)){
resultData.add(userModel);
}
}
filterResults.count = resultData.size();
filterResults.values = resultData;
}
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
userModelList = (List<UserModel>) filterResults.values;
notifyDataSetChanged();
}
};
return filter;
}
在您的主要活动中,将搜索视图和侦听器添加到onQueryTextChange。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem menuItem = menu.findItem(R.id.search_view);
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
usersAdapter.getFilter().filter(newText);
return true;
}
});
return true;
}
完整的教程和源代码。 Recyclerview with SearchView