如果搜索查询在搜索视图中不匹配,如何为回收者视图设置过滤器

How to set filter for recycler view if search query resulting no match in search view

如果我们在搜索视图中输入的项目匹配,但如果我们输入一些不匹配的查询,此代码可以正常工作...应用程序在 android 中崩溃。

此处Main_ATMItemList是返回整个结果列表的列表。 请帮我添加我需要添加的内容,以便即使查询不匹配,代码也能正常工作。

我已经在适配器中创建了这个方法class

public void setFilter(String queryText) {
        visibleObjects = new ArrayList<>();

            for (RouteByATMList.Route_ATM item : Main_ATMItemList) {
                if (item.ATMNumber.startsWith(queryText))
                {
                    visibleObjects.add(item);

                }
            }
        }

        notifyDataSetChanged();
        Log.e("dataset changed","dataset changed");
    }

您需要抽象RecyclerView.Adapter并使其继承Filterable。然后你将这个抽象subclass 到你想要创建的适配器。

覆盖 getFilter() 方法以实例化 Filter。在 Filter

performFiltering(String constraint) 方法中参考如何过滤适配器并移动您现在拥有的逻辑的示例

您应该在后台线程中进行过滤,因此 FilterableFilterable 在后台线程上进行过滤,并在 UI 线程或调用过滤的线程上传递结果。

像这样你可以在一定程度上兼容旧的ListView

评论里有人问例子:

public abstract class BaseFilterableRecyclerViewAdapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> implements Filterable {

    private Context mContext;
    public BaseFilterableRecyclerViewAdapter(Context context) {
        this.mContext = context;
    }
    public abstract void sort(SortingFilter.Sort sortingStrategy);

}

在扩展中class:

public class ProductAdapter extends BaseFilterableRecyclerViewAdapter<RecyclerView.ViewHolder>{

//------- Other methods ----

    @Override
    public Filter getFilter() {
        return new SortingFilter(mData) {
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                if (results.values != null) {
                    int last = mData.size();
                    mData = (List<? extends Product>) results.values;
                    notifyItemRangeChanged(mHeaderView == null ? 0 : 1, last);
                }
            }
        };
    }


}

一直使用这个对我有用

private List<ExampleModel> filter(List<ExampleModel> models, String query) {
    query = query.toLowerCase();

    final List<ExampleModel> filteredModelList = new ArrayList<>();
    for (ExampleModel model : models) {
        final String text = model.getText().toLowerCase();
        if (text.contains(query)) {
            filteredModelList.add(model);
        }
    }
    return filteredModelList;
}

然后onQueryTextChange(String query)调用过滤器

final List<ExampleModel> filteredModelList = filter(mModels, query);
        mAdapter.animateTo(filteredModelList);
        mRecyclerView.scrollToPosition(0);

下面是 Adapter 中的 animateTo 方法

 public void animateTo(List<ExampleModel> models) {
        applyAndAnimateRemovals(models);
        applyAndAnimateAdditions(models);
        applyAndAnimateMovedItems(models);
    }

private void applyAndAnimateRemovals(List<ExampleModel> newModels) {
    for (int i = mModels.size() - 1; i >= 0; i--) {
        final ExampleModel model = mModels.get(i);
        if (!newModels.contains(model)) {
            removeItem(i);
        }
    }
}

private void applyAndAnimateAdditions(List<ExampleModel> newModels) {
    for (int i = 0, count = newModels.size(); i < count; i++) {
        final ExampleModel model = newModels.get(i);
        if (!mModels.contains(model)) {
            addItem(i, model);
        }
    }
}

private void applyAndAnimateMovedItems(List<ExampleModel> newModels) {
    for (int toPosition = newModels.size() - 1; toPosition >= 0; toPosition--) {
        final ExampleModel model = newModels.get(toPosition);
        final int fromPosition = mModels.indexOf(model);
        if (fromPosition >= 0 && fromPosition != toPosition) {
            moveItem(fromPosition, toPosition);
        }
    }
}

public ExampleModel removeItem(int position) {
    final ExampleModel model = mModels.remove(position);
    notifyItemRemoved(position);
    return model;
}

public void addItem(int position, ExampleModel model) {
    mModels.add(position, model);
    notifyItemInserted(position);
}

public void moveItem(int fromPosition, int toPosition) {
    final ExampleModel model = mModels.remove(fromPosition);
    mModels.add(toPosition, model);
    notifyItemMoved(fromPosition, toPosition);
}

}