在使用filterable设置recyclerview之后,无法调用adapter.getFilter()

时间:2016-01-22 03:36:08

标签: search android-recyclerview

我在RecyclerView中使用了Filterable。我想要做的是,从edittext过滤recyclelerview。以下是我的实际代码。

public class NavDrawerAdapter_v2 extends RecyclerView.Adapter<NavDrawerAdapter_v2.ViewHolder> implements Filterable{

private static final int TYPE_ITEM = 0;

private ArrayList<String> menu_list;
private ArrayList<String> filtered_menu_list;
Context mContext;

public static class ViewHolder extends RecyclerView.ViewHolder {
    int Holderid;

    TextView shopName, shopLevel;

    public ViewHolder(View itemView,int ViewType) {
        super(itemView);

        shopName = (TextView) itemView.findViewById(R.id.rowText);
        shopLevel = (TextView) itemView.findViewById(R.id.rowLevel);

        if(ViewType == TYPE_ITEM) {
            Holderid = 0;
        }
    }
}

public NavDrawerAdapter_v2(Context mContext, ArrayList<String> menu_list){
    this.menu_list = menu_list;
    this.filtered_menu_list = menu_list;
    this.mContext = mContext;
}

@Override
public NavDrawerAdapter_v2.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    if (viewType == TYPE_ITEM) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.drawer_item_row,parent,false); //Inflating the layout
        return new ViewHolder(v,viewType);
    }

    return null;
}

@Override
public void onBindViewHolder(NavDrawerAdapter_v2.ViewHolder holder, int position) {

    if (holder.Holderid == 0) {
        String getShopName = menu_list.get(position).split("-")[0];
        String getShopLevel = menu_list.get(position).split("-")[1];
        holder.shopName.setText(getShopName);
        holder.shopLevel.setText(getShopLevel);
    }
}

@Override
public int getItemCount() {
    return menu_list == null ? 0 : menu_list.size();
}

@Override
public long getItemId(int position) {
    return super.getItemId(position);
}

@Override
public int getItemViewType(int position) {

    return TYPE_ITEM;
}

public boolean okay(){
    return true;
}

@Override
public Filter getFilter() {
    return new UserFilter(this, menu_list);
}

private static class UserFilter extends Filter {

    private final NavDrawerAdapter_v2 adapter;

    private final ArrayList<String> originalList;

    private final ArrayList<String> filteredList;

    private UserFilter(NavDrawerAdapter_v2 adapter, ArrayList<String>originalList) {
        super();
        this.adapter = adapter;
        this.originalList = new ArrayList<>(originalList);
        this.filteredList = new ArrayList<>();
    }

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        filteredList.clear();
        final FilterResults results = new FilterResults();

        if (constraint.length() == 0) {
            filteredList.addAll(originalList);
        } else {
            final String filterPattern = constraint.toString().toLowerCase().trim();

            for (String getValue : originalList) {
                if (getValue.contains(filterPattern)) {
                    filteredList.add(getValue);
                }
            }
        }
        results.values = filteredList;
        results.count = filteredList.size();
        return results;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        adapter.filtered_menu_list.clear();
        adapter.filtered_menu_list.addAll((ArrayList<String>) results.values);
        adapter.notifyDataSetChanged();
    }
}

}

但是,在我的片段中。我无法调用adapter.getFilter()。这有什么不对?

2 个答案:

答案 0 :(得分:0)

如果您希望EditText用于searchView,则需要为EditText添加更改侦听器,如链接中所述:android on Text Change Listener

当文字改变时,你应该打电话给

adapter.getFilter().filter(newText);

或者,您应该尝试使用recyclerView在操作栏中实现searchview。为此,在 RecyclerView适配器中实施 getFilter()方法。您可以使用链接https://stackoverflow.com/a/10532898/1308990中实现的getFilter()方法。

menu.xml文件:

<item android:id="@+id/menuSearch"
  android:title="search"
  android:icon = "@android:drawable/ic_menu_search"
  app:actionViewClass = "android.support.v7.widget.SearchView"
  app:showAsAction = "always|collapseActionView"></item>

在onCreateOptionsMenu()方法中,编写以下代码:

MenuItem item = menu.findItem(R.id.menuSearch);
//SearchView searchView = (SearchView) item.getActionView(); 
SearchView searchView =  (SearchView) MenuItemCompat.getActionView(item); 

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

//Called when the query text is changed by the user.
@Override
public boolean onQueryTextChange(String newText) {
    Log.d("soda in MainActivity","onQueryTextChange method is called.");
    mutfagimFragment.adapter.getFilter().filter(newText);
    return false;
}
});

当用户更改文本时,请检查是否通过监视日志来调用<​​strong> onQueryTextChange()方法。如果未调用,请尝试将上述代码放在 onPrepareOptionsMenu()方法中,而不是 onCreateOptionsMenu()方法。

答案 1 :(得分:0)

在您的示例之后,我很确定您将对象分配给RecyclerView.Adapter变量,而不是Activity中的NavDrawerAdapter_v2变量。

RecyclerView.Adapter adapter = new NavDrawerAdapter_v2(menuList);

代替

NavDrawerAdapter_v2 adapter = new NavDrawerAdapter_v2(menuList);

NavDrawerAdapter_v2从RecyclerView.Adapter扩展而来,但同时实现了Filterable。

如果将对象分配给RecyclerView.Adapter变量,您仍然可以调用getItemCount(),onBindViewHolder()和onCreateViewHolder()(因为它们是RecyclerView.Adapter的抽象方法)

糟糕的是,由于它属于Filterable接口,因此您将无法调用getFilter()。