我的Fragment
上有recyclerview。当我从Recyclerview
删除最后一个项目时,该项目被删除,但在版式上可见。我需要手动刷新Fragment
,然后它消失了。仅在删除最后一个项目时才会发生这种情况,否则从RecyclerView中删除该项目就可以了。
这是使用AdapterClass
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView name;
private TextView number;
private TextView people;
private ImageView deleteReservation;
public ViewHolder(View itemView, final OnEntryClickListener listener) {
super(itemView);
name=(TextView)itemView.findViewById(R.id.name);
number=(TextView) itemView.findViewById(R.id.number);
people=(TextView)itemView.findViewById(R.id.people);
deleteReservation=(ImageView)itemView.findViewById(R.id.delete);
editReservation=(ImageView)itemView.findViewById(R.id.editReservation);
deleteReservation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(listener!=null) {
int position = originalList.indexOf(filter.get(getAdapterPosition()));
if (position != RecyclerView.NO_POSITION) {
listener.deleteReservation(position);
}
}
}
});
这是用于从RecyclerView删除项目的代码
@Override
public void deleteReservation(final int position) {
removePeople = current.get(position).getNoOfPeople();
removeId = current.get(position).getReserveid();
removeTime = current.get(position).getFullTime();
AlertDialog.Builder confirm = new AlertDialog.Builder(getActivity());
confirm.setTitle("Confirmation");
confirm.setMessage("Are you sure to cancel reservation ?");
confirm.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
removeFromReservation();
removeItem(position);
}
});
这是删除项目的方法。
public void removeItem(int position) {
current.remove(position);
adapter.notifyItemRemoved(position);
//adapter.notifyItemRangeChanged(position, current.size());
adapter.notifyDataSetChanged();
if(current.size()==0){
current.clear();
getData();
}
}
答案 0 :(得分:1)
尝试此功能,希望它对您有用
public void removeItemAt(int position) {
current.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, current.size());
}
答案 1 :(得分:0)
将ItemView连同position参数一起传递以使其不可见。我已经写了我的连接,希望它将对您有用!
listener.deleteReservation(position,itemView);
public void deleteReservation(final int position , View itemView){
.
.
.
public void onClick(DialogInterface dialog, int which) {
removeFromReservation();
removeItem(position,itemView); // there pass again
}
}
通过这种方法使ItemView不可见或消失...
public void removeItem(int position,itemView) {
current.remove(position);
adapter.notifyItemRemoved(position);
//adapter.notifyItemRangeChanged(position, current.size());
itemView.setVisibility(View.GONE);
adapter.notifyDataSetChanged();
if(current.size()==0){
current.clear();
getData();
}
}