我一直試圖將SearchView添加到我的RecyclerView一段時間,並引用了這些帖子here,here和here。在這一點上,我相信答案正在我的臉上。SearchView與RecyclerView
但是,我不知道如何實現時,例如,使用搜索查看以下內容:
private static final List<DataModel> getDummyData(){
List<DataModel> dummyDataList = new ArrayList<>();
dummyDataList.add(new DataModel("Alphabet", "Sub Alphabet"));
dummyDataList.add(new DataModel("Banana", "Sub Banana"));
dummyDataList.add(new DataModel("Captain", "Sub Captain"));
dummyDataList.add(new DataModel("Donut", "Sub Donut"));
dummyDataList.add(new DataModel("Elephant", "Sub Elephant"));
dummyDataList.add(new DataModel("Fox", "Sub Fox"));
dummyDataList.add(new DataModel("Giraffe", "Sub Giraffe"));
dummyDataList.add(new DataModel("Hippo", "Sub Hippo"));
dummyDataList.add(new DataModel("Iguana", "Sub Iguana"));
dummyDataList.add(new DataModel("Jumanji", "Sub Jumanji"));
return dummyDataList;
}
這裏是我的當前設置(following this tutorial),使用Locale.getISOCountries();並搜索國家的名稱。
MainActivityFragment:
public class MainActivityFragment extends Fragment implements SearchView.OnQueryTextListener {
private RecyclerView mRecyclerView;
private List<DataModel> mDataModel;
private RVAdapter adapter;
public MainActivityFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_one_fragment, container, false);
mRecyclerView = (RecyclerView)view.findViewById(R.id.recyclerview);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(layoutManager);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setHasOptionsMenu(true);
String[] locales = Locale.getISOCountries();
mDataModel = new ArrayList<>();
for (String countryCode : locales){
Locale obj = new Locale("", countryCode);
mDataModel.add(new DataModel(obj.getDisplayCountry(), obj.getISO3Country()));
}
adapter = new RVAdapter(mDataModel);
mRecyclerView.setAdapter(adapter);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main, menu);
final MenuItem item = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(this);
MenuItemCompat.setOnActionExpandListener(item,
new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Do something when collapsed
adapter.setFilter(mDataModel);
return true;
}
});
}
@Override
public boolean onQueryTextChange(String newText) {
final List<DataModel> filteredModeList = filter(mDataModel, newText);
adapter.setFilter(filteredModeList);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
private List<DataModel>filter(List<DataModel> models, String query){
query = query.toLowerCase();
final List<DataModel> filteredModeList = new ArrayList<>();
for (DataModel model : models){
final String text = model.getName().toLowerCase();
if (text.contains(query)){
filteredModeList.add(model);
}
}
return filteredModeList;
}
}
而且我Adapter-RVAdapter.java:
public class RVAdapter extends RecyclerView.Adapter<ItemViewHolder> {
private List<DataModel> mDataModel;
public RVAdapter(List<DataModel> mDataModel){
this.mDataModel = mDataModel;
}
@Override
public void onBindViewHolder(ItemViewHolder itemViewHolder, int position) {
final DataModel model = mDataModel.get(position);
itemViewHolder.bind(model);
}
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int position){
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row, viewGroup, false);
return new ItemViewHolder(view);
}
@Override
public int getItemCount() {
return mDataModel.size();
}
public void setFilter(List<DataModel> dataModels){
mDataModel = new ArrayList<>();
mDataModel.addAll(dataModels);
notifyDataSetChanged();
}
}
這裏的ItemViewHolder.java:
public class ItemViewHolder extends RecyclerView.ViewHolder {
public TextView name_TextView;
public TextView subName_TextView;
public ItemViewHolder(View itemView){
super(itemView);
name_TextView = (TextView)itemView.findViewById(R.id.country_name);
subName_TextView = (TextView)itemView.findViewById(R.id.country_iso);
}
public void bind(DataModel dataModel){
name_TextView.setText(dataModel.getName());
subName_TextView.setText(dataModel.getSubName());
}
}
最後,DataModel.java:
public class DataModel {
String name;
String subName;
DataModel(String name, String subName){
this.name = name;
this.subName = subName;
}
public String getName(){
return name;
}
public String getSubName(){
return subName;
}
}
任何幫助,建議或在正確的方向點將是真棒!
您是否曾經爲此找到答案? ....好名字先生建築師;) – dazza5000
還不幸 - 我打算再採取一些裂縫。如果我有任何進展,將會更新。 – ImArtVandalay