带有RecyclerView的SearchView

问题描述 投票:0回答:3

我一直在尝试将SearchView添加到RecyclerView中,并引用了这些帖子hereherehere。在这一点上,我确定答案正盯着我。

但是,例如,使用以下内容时,我不确定如何实现SearchView:

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;
    }

这是我当前使用Locale.getISOCountries();的设置(following this tutorial;并搜索国家名称。

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;
    }
}

任何帮助,建议或正确方向的观点都将非常棒!

android android-recyclerview searchview
3个回答
0
投票

这是受以下帖子的启发:

How to filter a RecyclerView with a SearchView

我需要一个稍微复杂的解决方案,因为recyclerview托管在一个片段中,所以我使用EventBus将搜索查询从托管searchview的活动传递到该片段。

从有权访问SearchView的活动中>

@Override
public boolean onQueryTextChange(String query) {
    EventBus.getDefault().post(new SearchEvent(query));
    return true;
}

在托管recyclerview的片段上:

public void onEvent(SearchEvent e) {
    final List<? extends GenericCompany> filteredModelList = filter(mCompanies, e.getSearchString());
    mListAdapter.replaceData(filteredModelList);
    mRecyclerView.scrollToPosition(0);
}


private List<? extends GenericCompany> filter(List<SCSponsors> models, String query) {
    query = query.toLowerCase();

    final List<SCSponsors> filteredModelList = new ArrayList<>();
    for (SCSponsors model : models) {
        final String text = model.getDisplayName().toLowerCase();
        if (text.contains(query)) {
            filteredModelList.add(model);
        }
    }
    return filteredModelList;
}

在适配器内部:

    public void replaceData(List<? extends GenericCompany> companies) {
        setList(companies);
        notifyDataSetChanged();
    }

0
投票

好吧,所以仔细看一下here帖子(此答案很大程度上基于且不能没有这个答案,我得出以下结论:


0
投票

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.