gpt4 book ai didi

android - 如何从过滤列表中滑动删除项目? [回收 View ]

转载 作者:行者123 更新时间:2023-11-29 18:44:25 27 4
gpt4 key购买 nike

在我使用 SearchView 搜索项目后尝试从 RecyclerView 中删除项目时遇到问题,而如果我不使用 SearchView 并正常删除该项目,它工作正常。

这是一个示例,我正在删除该项目,我正在搜索 2 删除它,但它仍然保留在列表中。

enter image description here

虽然这是我在 Activity 中删除滑动项目的方法

  new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return false;
}

@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {

if (direction == ItemTouchHelper.RIGHT) {
itemAdapter.removeItem( viewHolder.getAdapterPosition());
}

if (direction == ItemTouchHelper.LEFT) {
itemAdapter.removeItem( viewHolder.getAdapterPosition());
}

}
}).attachToRecyclerView(recyclerView);

}

虽然这里是我的适配器的代码

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ExampleViewHolder>  implements Filterable {


private ArrayList<ItemModel> variantiConstructors;
private ArrayList<ItemModel> mFilteredList;
private OnItemClickListener mListener;

public interface OnItemClickListener{
void onItemClick(int position);
}

public void setOnItemClickListener(OnItemClickListener listener){
mListener = listener;
}

@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, mListener);
}

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, final OnItemClickListener listener) {
super(itemView);

desc = itemView.findViewById(R.id.Desc);
qta = itemView.findViewById(R.id.Qta);


itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(listener != null){
int position = getAdapterPosition();
if(position != RecyclerView.NO_POSITION){
listener.onItemClick(position);
}
}
}
});


}
}

public ArrayList<ItemModel> getList(){
return variantiConstructors;
}

public void removeItem(int position) {
variantiConstructors.remove(position);
notifyDataSetChanged();
}
}

最佳答案

试试这个,我已经对你的代码做了一些修改

MainActivity code

public class MainActivity extends AppCompatActivity {

ItemModel itemModel;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


findViewById(R.id.btnShow).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showCustomDialog();
}
});

}

private void showCustomDialog() {

final Dialog dialog = new Dialog(this);
final ArrayList<ItemModel> arrayList = new ArrayList<>();


dialog.setContentView(R.layout.custom_dialog);

RecyclerView myRecyclerView = dialog.findViewById(R.id.myRecyclerView);

ImageView ivDelete = dialog.findViewById(R.id.ivDelete);
ImageView ivClose = dialog.findViewById(R.id.ivClose);

final EditText edtSearch = dialog.findViewById(R.id.edtSearch);

arrayList.add(new ItemModel("Nilesh", String.valueOf(1)));
arrayList.add(new ItemModel("NEEL", String.valueOf(1)));
arrayList.add(new ItemModel("Prem", String.valueOf(1)));
arrayList.add(new ItemModel("Rathod", String.valueOf(1)));
arrayList.add(new ItemModel("Android", String.valueOf(1)));
arrayList.add(new ItemModel("Kanudo", String.valueOf(1)));


myRecyclerView.setHasFixedSize(true);
myRecyclerView.setLayoutManager(new LinearLayoutManager(this));

ivClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});

final ItemAdapter itemAdapter = new ItemAdapter(arrayList);
myRecyclerView.setAdapter(itemAdapter);

new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return false;
}

@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {

if (direction == ItemTouchHelper.RIGHT || direction == ItemTouchHelper.LEFT) {
itemModel = itemAdapter.getModel();
itemAdapter.removeItem(viewHolder.getAdapterPosition());


}

}
}).attachToRecyclerView(myRecyclerView);

edtSearch.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) {

if (edtSearch.getText().toString().isEmpty()) {
if (itemModel != null)
arrayList.remove(itemModel);
}
itemAdapter.getFilter().filter(edtSearch.getText().toString().toLowerCase());



}

@Override
public void afterTextChanged(Editable s) {

}
});

dialog.show();

}

}

ItemAdapter code

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ExampleViewHolder> implements Filterable {


private ArrayList<ItemModel> variantiConstructors;
private ArrayList<ItemModel> mFilteredList;
private OnItemClickListener mListener;
ItemModel itemModel;

ItemAdapter(ArrayList<ItemModel> exampleList) {
variantiConstructors = exampleList;
mFilteredList = variantiConstructors;
}


public interface OnItemClickListener {
void onItemClick(int position);
}

public void setOnItemClickListener(OnItemClickListener listener) {
mListener = listener;
}

@NonNull
@Override
public ItemAdapter.ExampleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item_layout, parent, false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
v.setLayoutParams(lp);
return new ItemAdapter.ExampleViewHolder(v, mListener);
}


@Override
public void onBindViewHolder(@NonNull final ExampleViewHolder holder, @SuppressLint("RecyclerView") final int position) {
ItemModel item = variantiConstructors.get(position);

holder.desc.setText(item.getItemName());
holder.qta.setText(item.getQty());

itemModel = item;

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.getItemName().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 TextView qta;

ExampleViewHolder(View itemView, final OnItemClickListener listener) {
super(itemView);

desc = itemView.findViewById(R.id.itemName);
qta = itemView.findViewById(R.id.Qta);


itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
listener.onItemClick(position);
}
}
}
});


}
}

public ItemModel getModel() {
return itemModel;

}

public ArrayList<ItemModel> getList() {
return variantiConstructors;
}

public void removeItem(int position) {
variantiConstructors.remove(position);
notifyDataSetChanged();
}
}

输出

https://www.youtube.com/watch?v=T80bk0756xk

关于android - 如何从过滤列表中滑动删除项目? [回收 View ],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52298470/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com