How to filter a RecyclerView with a SearchView

后端 未结 11 1864
孤独总比滥情好
孤独总比滥情好 2020-11-22 00:28

I am trying to implement the SearchView from the support library. I want the user to be to use the SearchView to filter a List of movi

11条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 00:33

    I have solved the same problem using the link with some modifications in it. Search filter on RecyclerView with Cards. Is it even possible? (hope this helps).

    Here is my adapter class

    public class ContactListRecyclerAdapter extends RecyclerView.Adapter implements Filterable {
    
    Context mContext;
    ArrayList customerList;
    ArrayList parentCustomerList;
    
    
    public ContactListRecyclerAdapter(Context context,ArrayList customerList)
    {
        this.mContext=context;
        this.customerList=customerList;
        if(customerList!=null)
        parentCustomerList=new ArrayList<>(customerList);
    }
    
       // other overrided methods
    
    @Override
    public Filter getFilter() {
        return new FilterCustomerSearch(this,parentCustomerList);
    }
    }
    

    //Filter class

    import android.widget.Filter;
    import java.util.ArrayList;
    
    
    public class FilterCustomerSearch extends Filter
    {
    private final ContactListRecyclerAdapter mAdapter;
    ArrayList contactList;
    ArrayList filteredList;
    
    public FilterCustomerSearch(ContactListRecyclerAdapter mAdapter,ArrayList contactList) {
        this.mAdapter = mAdapter;
        this.contactList=contactList;
        filteredList=new ArrayList<>();
    }
    
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        filteredList.clear();
        final FilterResults results = new FilterResults();
    
        if (constraint.length() == 0) {
            filteredList.addAll(contactList);
        } else {
            final String filterPattern = constraint.toString().toLowerCase().trim();
    
            for (final Contact contact : contactList) {
                if (contact.customerName.contains(constraint)) {
                    filteredList.add(contact);
                }
                else if (contact.emailId.contains(constraint))
                {
                    filteredList.add(contact);
    
                }
                else if(contact.phoneNumber.contains(constraint))
                    filteredList.add(contact);
            }
        }
        results.values = filteredList;
        results.count = filteredList.size();
        return results;
    }
    
    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        mAdapter.customerList.clear();
        mAdapter.customerList.addAll((ArrayList) results.values);
        mAdapter.notifyDataSetChanged();
    }
    

    }

    //Activity class

    public class HomeCrossFadeActivity extends AppCompatActivity implements View.OnClickListener,OnFragmentInteractionListener,OnTaskCompletedListner
    {
    Fragment fragment;
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_homecrossfadeslidingpane2);CardView mCard;
       setContentView(R.layout.your_main_xml);}
       //other overrided methods
      @Override
       public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
    
        MenuInflater inflater = getMenuInflater();
        // Inflate menu to add items to action bar if it is present.
        inflater.inflate(R.menu.menu_customer_view_and_search, menu);
        // Associate searchable configuration with the SearchView
        SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView =
                (SearchView) menu.findItem(R.id.menu_search).getActionView();
        searchView.setQueryHint("Search Customer");
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));
    
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }
    
            @Override
            public boolean onQueryTextChange(String newText) {
                if(fragment instanceof CustomerDetailsViewWithModifyAndSearch)
                    ((CustomerDetailsViewWithModifyAndSearch)fragment).adapter.getFilter().filter(newText);
                return false;
            }
        });
    
    
    
        return true;
    }
    }
    

    In OnQueryTextChangeListener() method use your adapter. I have casted it to fragment as my adpter is in fragment. You can use the adapter directly if its in your activity class.

提交回复
热议问题