我正在尝试从RecyclerView上轻扫项目,但是当我删除它并关闭搜索时,被删除的项目仍然保留在列表中,但是当我在“搜索”中对其进行擦除时,它消失了。
如果我不使用搜索并在打开recyclerView时删除该项目,则可以将其删除。当我搜索并删除项目时就会出现问题。
这是我的适配器代码:
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import java.util.ArrayList;
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ExampleViewHolder> implements Filterable {
private ArrayList<ItemModel> variantiConstructors;
private ArrayList<ItemModel> mFilteredList;
@NonNull
@Override
public ItemAdapter.ExampleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler,parent,false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
v.setLayoutParams(lp);
return new ItemAdapter.ExampleViewHolder(v);
}
ItemAdapter(ArrayList<ItemModel> exampleList){
variantiConstructors = exampleList;
mFilteredList = variantiConstructors;
}
@Override
public void onBindViewHolder(@NonNull final ItemAdapter.ExampleViewHolder holder, @SuppressLint("RecyclerView") final int position) {
ItemModel item = variantiConstructors.get(position);
holder.desc.setText(item.getCodiceArticolo());
holder.qta.setText(item.getQta());
// holder.qta.addTextChangedListener(new TextWatcher() {
// @Override
// public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
// }
//
// @Override
// public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
// variantiConstructors.get(position).setQta(holder.qta.getText().toString());
// }
//
// @Override
// public void afterTextChanged(Editable editable) {
// }
// });
if(position % 2 == 0 ){
holder.itemView.setBackgroundColor(Color.parseColor("#5C5C5C"));
}else if(position % 2 == 1){
holder.itemView.setBackgroundColor(Color.parseColor("#9E9E9E"));
}
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults oReturn = new FilterResults();
final ArrayList<ItemModel> results = new ArrayList<>();
if (mFilteredList == null)
mFilteredList = new ArrayList<>(variantiConstructors);
if (constraint != null && constraint.length() > 0) {
if (mFilteredList != null && mFilteredList.size() > 0) {
for (final ItemModel cd : mFilteredList) {
if (cd.getCodiceArticolo().toLowerCase()
.contains(constraint.toString().toLowerCase()))
results.add(cd);
}
}
oReturn.values = results;
oReturn.count = results.size(); //newly Aded by ZA
} else {
oReturn.values = mFilteredList;
oReturn.count = mFilteredList.size(); //newly added by ZA
}
return oReturn;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(final CharSequence constraint,
FilterResults results) {
variantiConstructors = new ArrayList<>((ArrayList<ItemModel>) results.values);
notifyDataSetChanged();
}
};
}
@Override
public int getItemCount() {
return variantiConstructors.size();
}
public class ExampleViewHolder extends RecyclerView.ViewHolder {
public TextView desc;
public EditText qta;
ExampleViewHolder(View itemView) {
super(itemView);
desc = itemView.findViewById(R.id.Desc);
qta = itemView.findViewById(R.id.Qta);
}
}
public ArrayList<ItemModel> getList(){
return variantiConstructors;
}
public void removeItem(int position){
variantiConstructors.remove(position);
notifyDataSetChanged();
}
public ArrayList<ItemModel> getmFilteredList() {
return mFilteredList;
}
}