从RecyclerView中删除的最后一项,但在版式上仍然可见

时间:2018-07-30 12:30:46

标签: android android-fragments android-recyclerview

我的Fragment上有。当我从Recyclerview删除最后一个项目时,该项目被删除,但在版式上可见。我需要手动刷新Fragment,然后它消失了。仅在删除最后一个项目时才会发生这种情况,否则从RecyclerView中删除该项目就可以了。
这是使用AdapterClass

中的接口调用Delete方法的代码
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();
        }

    }

2 个答案:

答案 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();
        }

    }