7

我想通過RecyclerView搜索,我有List<BaseOfCards>(BaseOfCards是我的getter &二傳手類) 我RecyclerViewAdapter搜索通過RecyclerView使用搜索查看

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> { 


private LayoutInflater inflater; 
private List<BaseOfCards> items; 

//private int itemLayout; 
//String cardvalue; 
private Activity mActivity; 

public RecyclerViewAdapter(Activity mActivity, Context context, List<BaseOfCards> items) { 
    this.mActivity = mActivity; 
    inflater = LayoutInflater.from(context); 
    this.items = items; 
    //this.itemLayout = itemLayout; 
} 

@Override 
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

    View view = inflater.inflate(R.layout.custom_row, parent, false); 
    MyViewHolder holder = new MyViewHolder(view, mActivity); 
    return holder; 
} 

@Override 
public void onBindViewHolder(MyViewHolder holder, int position) { 

    BaseOfCards item = items.get(position); 
    holder.title.setTag(item); 
    holder.title.setText(item.getCardName()); 
} 

@Override 
public int getItemCount() { 

    return items.size(); 
} 

public static class MyViewHolder extends RecyclerView.ViewHolder { 
    private Activity mActivity; 
    TextView title; 
    ImageView titileImageView; 

    public MyViewHolder(View itemView, Activity mActivity) { 

     super(itemView); 

     titileImageView = (ImageView) itemView.findViewById(R.id.image_country); 


     title = (TextView) itemView.findViewById(R.id.listText); 
     this.mActivity = mActivity; 

    } 
} 

} 

我添加搜索查看我的菜單,並在初始化MainActivity

MenuItem menuItem = menu.findItem(R.id.action_search1); 
    searchView = (SearchView) MenuItemCompat.getActionView(menuItem); 
    SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
    searchView.setIconifiedByDefault(true); 

我需要什麼,下一步該怎麼做?讓我的RecyclerViewAdapterimplement Filterable還是什麼?或者只是在* RecyclerViewAdapter **中創建類filter,並從我的MainActivity中調用它?

回答

19

我解決我的問題

  1. 讓我的課RecyclerViewAdapterimplements Filterable

  2. 添加行private List<BaseOfCards> orig;

  3. Add方法getFilterRecyclerViewAdapter

    public Filter getFilter() { 
    return new Filter() { 
        @Override 
        protected FilterResults performFiltering(CharSequence constraint) { 
         final FilterResults oReturn = new FilterResults(); 
         final List<BaseOfCards> results = new ArrayList<BaseOfCards>(); 
         if (orig == null) 
          orig = items; 
          if (constraint != null){ 
           if(orig !=null & orig.size()>0){ 
            for (final BaseOfCards g :orig) { 
             if (g.getCardName().toLowerCase().contains(constraint.toString()))results.add(g); 
            } 
           } 
           oReturn.values = results; 
          } 
          return oReturn; 
         } 
    
    @Override 
        protected void publishResults(CharSequence constraint, FilterResults results) { 
         items = (ArrayList<BaseOfCards>)results.values; 
         notifyDataSetChanged(); 
    
        } 
    }; 
    
  4. MainActivityimplements SearchView.OnQueryTextListener和改變方法onQueryTextChange

    @Override 
    public boolean onQueryTextChange(String newText) { 
        if (TextUtils.isEmpty (newText)) { 
         adapter.getFilter().filter(""); 
        } else { 
         adapter.getFilter().filter(newText.toString()); 
        } 
        return true; 
    } 
    
+0

很酷,我會在我的https://github.com/davideas/FlexibleAdapter ;-)中加入這個功能('getFilter');-) – Davidea 2015-07-20 21:03:04

+0

@Davidea哦,非常棒,非常感謝! – Ololoking 2015-07-20 21:59:33

+0

我添加了這個功能,但它比預想的更復雜。事實上,我不相信在適配器中有這樣的過濾器,因爲適配器中的對象已經是**原始內容中來自諸如DB/XML/JSON之類的源的副本,作爲域對象 - 問題不在過濾器上,而是在**過濾的**列表上執行的add/del/mod:所有更改都應該反映在「原始」適配器列表中** AND **在原始源代碼中... At此點更方便直接從源和更新適配器與該列表中篩選複製列表。 – Davidea 2015-07-21 11:56:58

0

使用一個autocompletetextview或一個EditText我處理這一個如下其中

public List<SalesProductsItems> mItems   

是初始列表項實例和。

public static List<SalesProductsItems> filteredIt 

在顯示items.Since所述第一時間的過濾器的結果是不是空的mItems實例將等於filteredIt實例使用的實例(而喪失的初始列表)然後在publishResults方法mItems前右失去原始值,我將它與傳遞的實例originallist等同起來。希望它可以幫助別人

private static class ProductsFilter extends Filter { 

    private final SalesProductsAdapter adapter; 

    private final List<SalesProductsItems> originalList; 

    private final List<SalesProductsItems> filteredList; 

    private ProductsFilter(SalesProductsAdapter adapter, List<SalesProductsItems> originalList) { 
     super(); 
     this.adapter = adapter; 
     this.originalList = new LinkedList<>(originalList); 
     this.filteredList = new ArrayList<>(); 
    } 

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

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

      for (final SalesProductsItems it : originalList) { 

       if (it.getProduct().toLowerCase().contains(filterPattern)) { 
        filteredList.add(it); 
       } 
      } 
     } 

     results.values = filteredList; 
     results.count = filteredList.size(); 
     return results; 
    } 

    @Override 
    protected void publishResults(CharSequence constraint, FilterResults results) { 
     adapter.mItems = originalList; 
     if(results.count > 0) { 
      filteredIt.clear(); 
      filteredIt.addAll((ArrayList<SalesProductsItems>) results.values); 
      adapter.notifyDataSetChanged(); 
     } else { 
      filteredIt.clear(); 
      filteredIt.addAll(adapter.mItems); 
      adapter.notifyDataSetChanged(); 
     } 
    } 
} 
0

我想補充到ololoking答案。 在MainActivity還應加上下面的代碼,以便它會工作:

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.menu_layout, menu); 

    MenuItem searchItem = menu.findItem(R.id.action_search); 
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem); 
    SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
    searchView.setIconifiedByDefault(true); 
    searchView.setOnQueryTextListener(this); 
    return super.onCreateOptionsMenu(menu); 

} 

感謝ololoking你的答案。它幫助了我。

0

從另一方面肯定的回答做的時候,我現在已經在我FlexibleAdapter庫中實現一個快速異步濾波器使用AsyncTask,表現都非常好,大名單,有動畫呢! 該適配器可配置爲啓用/禁用過濾結果中的屬性,以在必要時提高速度。另一大優勢還在於界面仍然對用戶做出響應。

在運行Android 6的我的三星S3中完成的測試:從10.450項目的起始列表開始,從後臺進程開始的那一刻起,需要花費大約1秒來篩選一個字符並選擇3.890項目。

我也做了一個Wiki page與所有的細節使用過濾器與適配器。

+0

我是同樣的問題,你解決了這個問題。請幫助我@Davidea – 2017-12-29 07:44:18

+0

@SagarHudge,從我以前的評論時間到接受的答案,我的過濾器發生了變化,目前的過濾方式在wiki頁面中有描述。空間做得更好,並且與Filter類相似,但現在這是FlexibleAdapter的方式。 – Davidea 2017-12-29 16:59:38