Search code examples
androidandroid-recyclerviewsearchview

SubmitQuery on RecyclerView with images very slow and not work correctly


Good day for everybody, I implemented RecyclerView with SearchView and the data from web service, In SearchView when i clear the some of text or all of text nothing update list and it's very slow to execute query, I follow this answer .

Note : items are contains images !

my filter is very simple does not complex :

    private ArrayList<itemObj> filter(ArrayList<itemObj> models, String query) {
    final ArrayList<itemObj> filteredModelList = new ArrayList<>();
    for (itemObj model : models) {
        final String text = model.getTitle();
        if (text.contains(query)) {
            filteredModelList.add(model);
        }
    }
    return filteredModelList;
}

any help ?


Solution

  • The problem here is after you cleared the searched text, the list back of adapter has smaller size than filter list. So the code need to modify as below

    public void addItem(int position, ExampleModel model) {
        if(position >= mModel.size()) {
            mModel.add(model);
            notifyItemInserted(mModel.size()-1);
        } else {
            mModels.add(position, model);
            notifyItemInserted(position);
        }
    }
    

    And modify also in moveItem functionality

    public void moveItem(int fromPosition, int toPosition) {
        final ExampleModel model = mModels.remove(fromPosition);
        if(toPosition >= mModels.size()) {
            mModels.add(model);
            notifyItemMoved(fromPosition, mModels.size()-1);
        } else {
            mModels.add(toPosition, model);
            notifyItemMoved(fromPosition, toPosition); 
        }
    }
    

    Hope that It could help you!