2017-06-27 65 views
-11

私有類ValueFilter擴展器{Android的工作室:如何在recyclerview添加過濾器和如何實現它

@Override 
    protected FilterResults performFiltering(CharSequence constraint) { 
     FilterResults results = new FilterResults(); 

     dbList = itemListCopy; 

     if (constraint != null && constraint.length() > 0) { 
      List<DbItemModel> filterList = new ArrayList(); 
      for (int i = 0; i < dbList.size(); i++) { 
       String str = (dbList.get(i).getItemDetails().toUpperCase()) + " " + 
         (dbList.get(i).getGroupName().toUpperCase()) + " " + 
         (dbList.get(i).getItemName().toUpperCase()) + " "; 
       if (str.contains(constraint.toString().toUpperCase())) { 
        filterList.add(dbList.get(i)); 
       } 
      } 
      dbList = filterList; 
      results.count = filterList.size(); 
      results.values = filterList; 
     } else { 
      results.count = itemListCopy.size(); 
      results.values = itemListCopy; 
     } 
     return results; 
    } 

    @Override 
    protected void publishResults(CharSequence charSequence, FilterResults results) { 
     dbList = (List<DbItemModel>) results.values; 
     notifyDataSetChanged(); 
    } 
} 

我想在我的recyclerview添加過濾器和使用它的搜索功能。 PLS。幫助我,如果你能讓它變得簡單。多謝你們。

+0

我投票關閉這一問題作爲題外話,因爲它是一個零效果的需要傾倒。 – EJoshuaS

+0

[爲什麼「有人可以幫助我?」不是一個真正的問題?](http://meta.stackoverflow.com/q/284236) – EJoshuaS

回答

0

步驟1實現可篩選接口適配器並執行其方法

例如:

public class MyAdapter extends 
    RecyclerView.Adapter<MyAdapter.ViewHolder> implements Filterable{ 
    // Other code 

    } 

步驟2創建延伸濾波器

類例如:

private class ValueFilter extends Filter { 

@Override 
protected FilterResults performFiltering(CharSequence constraint) { 
    FilterResults results = new FilterResults(); 
    contactList = contactsListCopy; 
// Replace with your own logic 
    if (constraint != null && constraint.length() > 0) { 
    List<Contacts> filterList = new ArrayList(); 
    for (int i = 0; i < contactList.size(); i++) { 
     if ((contactList.get(i).getName().toUpperCase()) 
      .contains(constraint.toString().toUpperCase())) { 
     filterList.add(contactList.get(i)); 
     } 
    } 
    contactList = filterList; 
    results.count = filterList.size(); 
    results.values = filterList; 
    } else { 
    results.count = contactsListCopy.size(); 
    results.values = contactsListCopy; 
    } 
    return results; 
} 

@Override 
protected void publishResults(CharSequence charSequence, FilterResults results) { 
    contactList = (List<Contacts>) results.values; 
    notifyDataSetChanged(); 
} 

}

步驟3:爲了簡單起見在片段/活動佈局上述再循環器視圖中的活動添加

<android.support.v7.widget.SearchView 
    android:id="@+id/search" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"/> 

步驟4 /片段實現SearchView.OnQueryTextListener和初始化視圖

SearchView sv = (SearchView) itemView.findViewById(R.id.search); 
sv.setVisibility(View.VISIBLE); 
sv.setQueryHint("Search Here"); 
sv.setOnQueryTextListener(this); 

步驟5:實現所有方法

步驟6更新像這樣的接口

 @Override 
    public boolean onQueryTextSubmit(String query) { 
    mAdapter.getFilter().filter(query); 
    return true; 
    } 

    @Override 
    public boolean onQueryTextChange(String newText) { 
    mAdapter.getFilter().filter(newText); 
    return true; 
} 
+0

嗨。只想在我現有的活動中嘗試您的代碼。並且我想要一個位於操作欄菜單中的可摺疊搜索按鈕在我的recyclerview上執行搜索/過濾器。它的代碼是什麼? –

+0

不好意思問這個(因爲我剛剛開始使用android和java編寫代碼),我應該在哪裏放置/鍵入ValueFilter類?我應該創建一個新的java類文件嗎? –

+0

只能在適配器類中輸出 – MRX

0

這裏是我的代碼

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.menus, menu); 
    MenuItem searchViewItem = menu.findItem(R.id.action_search); 
    final SearchView searchViewAndroidActionBar = (SearchView) MenuItemCompat.getActionView(searchViewItem); 
    searchViewAndroidActionBar.setOnQueryTextListener(new SearchView.OnQueryTextListener() { 

     @Override 
     public boolean onQueryTextSubmit(String query) { 
      mAdapter.getFilter().filter(query); 
      return true; 
     } 

     @Override 
     public boolean onQueryTextChange(String newText) { 
      return false; 
     } 

    }); 

    return super.onCreateOptionsMenu(menu); 
} 
相關問題