在 Recycler 视图上配置搜索视图
Configure Search View on Recycler View
当我在我的应用程序中搜索某些内容时,搜索项的文本已更改,因为搜索更改了它的位置...
我的适配器的OnBindViewHolder配置“setText”应该设置在一个位置,但是当我搜索东西时,搜索到的项目在Recycler View中的位置变成了第一个项目,所以setText改变了他的文本对于第一项文本。
错误
文本“receita torta”更改为“receita pudim”:
1: https://i.stack.imgur.com/F5CzW.gif
适配器
public class AdapterLanches extends RecyclerView.Adapter<AdapterLanches.MyViewHolder> implements Filterable {
private List<Lanches> listaLanches;
private List<Lanches> listaLanchesCompleta;
int estadoPrograma;
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView titulo;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
titulo = itemView.findViewById(R.id.textnomeLancheId);
}
}
public AdapterLanches(List<Lanches> listaLanches) {
this.listaLanches = listaLanches;
listaLanchesCompleta = new ArrayList<>(listaLanches);
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemListaLanches = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_lanches, parent, false);
return new MyViewHolder(itemListaLanches);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
Lanches itemAtual = listaLanches.get(position);
holder.titulo.setText(itemAtual.getTitulo());
}
@Override
public int getItemCount() {
return listaLanches.size();
}
@Override
public Filter getFilter() {
return lanchesFilter;
}
private Filter lanchesFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<Lanches> filteredList = new ArrayList<>();
if (constraint == null || constraint.length() == 0){
filteredList.addAll(listaLanchesCompleta);
}else{
estadoPrograma = 1;
String filterPattern = constraint.toString().toLowerCase().trim();
for ( Lanches item : listaLanchesCompleta){
if (item.getTitulo().toLowerCase().contains(filterPattern)){
filteredList.add(item);
}
}
}
FilterResults results = new FilterResults();
results.values = filteredList;
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
listaLanches.clear();
listaLanches.addAll( (List) results.values);
notifyDataSetChanged();
}
};
}
Lanches 上的搜索菜单 Activity
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_lanches, menu);
MenuItem searchItem = menu.findItem(R.id.pesquisaIcone);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setImeOptions(EditorInfo.IME_ACTION_DONE);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
AdapterLanches adapterLanches = new AdapterLanches(listaLanches);
adapterLanches.getFilter().filter(newText);
return false;
}
});
return true;
}
}
发生此错误是因为您没有为您的适配器使用相同的引用 class。
@Override
public boolean onQueryTextChange(String newText) {
AdapterLanches adapterLanches = new AdapterLanches(listaLanches);
adapterLanches.getFilter().filter(newText);
return false;
}
在此方法中,您应该使用与以前相同的引用,当您将列表提交到 RecyclerView 时,该方法也应该 return true。
蟒蛇朋友!
当我在我的应用程序中搜索某些内容时,搜索项的文本已更改,因为搜索更改了它的位置...
我的适配器的OnBindViewHolder配置“setText”应该设置在一个位置,但是当我搜索东西时,搜索到的项目在Recycler View中的位置变成了第一个项目,所以setText改变了他的文本对于第一项文本。
错误
文本“receita torta”更改为“receita pudim”:
适配器
public class AdapterLanches extends RecyclerView.Adapter<AdapterLanches.MyViewHolder> implements Filterable {
private List<Lanches> listaLanches;
private List<Lanches> listaLanchesCompleta;
int estadoPrograma;
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView titulo;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
titulo = itemView.findViewById(R.id.textnomeLancheId);
}
}
public AdapterLanches(List<Lanches> listaLanches) {
this.listaLanches = listaLanches;
listaLanchesCompleta = new ArrayList<>(listaLanches);
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemListaLanches = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_lanches, parent, false);
return new MyViewHolder(itemListaLanches);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
Lanches itemAtual = listaLanches.get(position);
holder.titulo.setText(itemAtual.getTitulo());
}
@Override
public int getItemCount() {
return listaLanches.size();
}
@Override
public Filter getFilter() {
return lanchesFilter;
}
private Filter lanchesFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<Lanches> filteredList = new ArrayList<>();
if (constraint == null || constraint.length() == 0){
filteredList.addAll(listaLanchesCompleta);
}else{
estadoPrograma = 1;
String filterPattern = constraint.toString().toLowerCase().trim();
for ( Lanches item : listaLanchesCompleta){
if (item.getTitulo().toLowerCase().contains(filterPattern)){
filteredList.add(item);
}
}
}
FilterResults results = new FilterResults();
results.values = filteredList;
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
listaLanches.clear();
listaLanches.addAll( (List) results.values);
notifyDataSetChanged();
}
};
}
Lanches 上的搜索菜单 Activity
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_lanches, menu);
MenuItem searchItem = menu.findItem(R.id.pesquisaIcone);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setImeOptions(EditorInfo.IME_ACTION_DONE);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
AdapterLanches adapterLanches = new AdapterLanches(listaLanches);
adapterLanches.getFilter().filter(newText);
return false;
}
});
return true;
}
}
发生此错误是因为您没有为您的适配器使用相同的引用 class。
@Override
public boolean onQueryTextChange(String newText) {
AdapterLanches adapterLanches = new AdapterLanches(listaLanches);
adapterLanches.getFilter().filter(newText);
return false;
}
在此方法中,您应该使用与以前相同的引用,当您将列表提交到 RecyclerView 时,该方法也应该 return true。
蟒蛇朋友!