我一直在尝试创建一个将使用搜索视图进行筛选的回收视图。它显示列表中我提供的所有项目,而不是筛选。我也在使用数据绑定。我犯了一个大错误,或者我错过了什么。有什么建议吗?
这是我的密码。
片段类
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;
}
});
}
SearchFragmentRecyclerViewAdapter类
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;
}
}
}
XML文件
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<variable
name="User"
type="com.batch.mcs.finalproject.models.User"/>
</data>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/AppTheme.ActivityLayoutStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/relativeLayout">
<android.support.v7.widget.SearchView
android:id="@+id/sv_search_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:background="@drawable/edit_text_background"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
android:maxLines="1"
app:layout_constraintBottom_toTopOf="@+id/rv_search_layout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_search_layout"
android:layout_width="match_parent"
android:layout_height="@dimen/no_dimension"
android:dividerHeight="@dimen/rv_search_group_margins"
android:paddingTop="@dimen/activity_horizontal_margin"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/sv_search_layout" />
</android.support.constraint.ConstraintLayout>
</layout>
使用此方法在RecyclerView中搜索,将此代码设置在Fragment中,而不是Adapter中。
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) {
}
});