尝试创建一个recyclerview,将使用searchview对其进行过滤

时间:2018-08-02 02:42:37

标签: android android-recyclerview searchview android-databinding

我一直在尝试创建一个recyclerview,将使用searchview对其进行过滤。它显示我提供的列表中的所有项目,而不是过滤。我也在使用数据绑定。我犯了一个大错误,或者我错过了一些东西。有什么建议吗?

这是我的代码。

片段类

>>> d("#hello")
[<p#hello.hello>]
>>> p = d("#hello")
>>> print(p.html())
Hello world !
>>> p.html("you know <a href='http://python.org/'>Python</a> rocks")
[<p#hello.hello>]
>>> print(p.html())
you know <a href="http://python.org/">Python</a> rocks
>>> print(p.text())
you know Python rocks

SearchFragmentRecyclerViewAdapter类

    package com.batch.mcs.finalproject;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.batch.mcs.finalproject.adapters.SearchFragmentRecyclerViewAdapter;
import com.batch.mcs.finalproject.databinding.FragmentSearchBinding;
import com.batch.mcs.finalproject.models.Group;
import com.batch.mcs.finalproject.viewmodel.AppViewModel;

import java.util.List;

public class SearchFragment extends BaseFragment {

    FragmentSearchBinding fragmentSearchBinding;
    View view;
    SearchFragmentRecyclerViewAdapter adapter;
    RecyclerView recyclerView;

    public static SearchFragment getInstance(){
        return new SearchFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    fragmentSearchBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_search, container, false);

    view = fragmentSearchBinding.getRoot();

    AppViewModel appViewModel = ViewModelProviders.of(this).get(AppViewModel.class);
    appViewModel.initMockGroups();
    appViewModel.getLiveGroupAll().observe(this, new Observer<List<Group>>() {
        @Override
        public void onChanged(@Nullable List<Group> groups) {

            SearchView searchView = fragmentSearchBinding.svSearchLayout;
            search(searchView);

            adapter = new SearchFragmentRecyclerViewAdapter(groups, getContext());
            recyclerView = fragmentSearchBinding.rvSearchLayout;
            recyclerView.setHasFixedSize(false);
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            recyclerView.setAdapter(adapter);
        }
    });

    return view;
}

private void search(SearchView searchView) {

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {

            adapter.getFilter().filter(newText);
            return true;
        }
    });
}

XML文件

    package com.batch.mcs.finalproject.adapters;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;

import com.batch.mcs.finalproject.BR;
import com.batch.mcs.finalproject.R;
import com.batch.mcs.finalproject.models.Group;

import java.util.ArrayList;
import java.util.List;

public class SearchFragmentRecyclerViewAdapter extends RecyclerView.Adapter<SearchFragmentRecyclerViewAdapter.RecyclerViewHolder> implements Filterable {

    private List<Group> groups;
    private List<Group> filteredGroups;
    private Context context;

    public SearchFragmentRecyclerViewAdapter(List<Group> groups, Context context){
        this.groups = groups;
        this.filteredGroups = groups;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View itemView = inflater.inflate(R.layout.recyclerview_group_information_item,viewGroup, false);
        return new RecyclerViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
        Group group = filteredGroups.get(position);
        holder.getBinding().setVariable(BR.groupItem, group);
    }

    @Override
    public int getItemCount() {
        return filteredGroups.size();
    }

    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence charSequence) {
                String charString = charSequence.toString();

                if(charString.isEmpty()){
                    filteredGroups = groups;
                } else {
                    List<Group> fGroups = new ArrayList<>();
                    for(Group g:fGroups){
                        if(g.getName().toLowerCase().contains(charString)){
                            fGroups.add(g);
                        }
                        filteredGroups = fGroups;
                    }
                }

                FilterResults filterResults = new FilterResults();
                filterResults.values = filteredGroups;
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
                filteredGroups = (ArrayList<Group>) filterResults.values;
                notifyDataSetChanged();
            }
        };
    }

    public class RecyclerViewHolder extends RecyclerView.ViewHolder {

        private ViewDataBinding binding;

        public RecyclerViewHolder(View itemView) {
            super(itemView);
            binding = DataBindingUtil.bind(itemView);
        }

        public ViewDataBinding getBinding() {
            return binding;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

使用此方法在RecyclerView中进行搜索,在片段而不是适配器中设置此代码。

searchBox.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mAdapterSmall.getFilter().filter(s.toString());
                mAdapterBig.getFilter().filter(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });