Search code examples
androidandroid-fragmentsfilterandroid-recyclerviewandroid-search

Android recyclerview filter showing no items until searchview icon is clicked


I have a fragment in which I do a network call and populate the contained recycler view with cards. Now I followed this to add a filter to my recycler view. Now the problem arises when in the adapter constructor I do

this.storeLists = new ArrayList<>(storeLists);

the List in my fragment gets populated but doesn't show any item until I click the searchview icon and start typing something. After that even if I close the searchview the list retains.

I tried changing the above line to

this.storeLists = storeLists;

when I remove the search query the deleted items are not re shown. So in case if I search something non - existent in my recycler view list, I am left with nothing showing in my recycler view.

What I want is that when I open the fragment I see the result and then when I click the search icon and start typing the filters work as shown in the link above.

EDIT:

Here is my adapter and here is my calling fragment.


Solution

  • After some hit and trial I found the solution at the cost of the cool animation. For now the code segment works by just resetting the adapter like this:

    @Override
    public boolean onQueryTextChange(String newText) {
        Log.d(TAG,newText);
        searchStoreList = storeList;
        final List<StoreList> filteredModelList = filter(searchStoreList, newText);
        //((RVStoreAdapter) mAdapter).animateTo(filteredModelList);
        //The above commented line is the old code. Following is how to reset the adapter!
        mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
        mRecyclerView.setLayoutManager(mLayoutManager);
        mAdapter = new RVStoreAdapter(getActivity(),storeList,session.getLat(),session.getLongi());
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.scrollToPosition(0);
        return true;
    }
    

    The animation is now very abrupt but the solution resets the list on changing the search query accordingly.