RecyclerView从RecylerView中删除后的最后一个项目可见

时间:2018-08-06 09:57:22

标签: android android-fragments android-recyclerview recycler-adapter

我的申请中有。我在Adapter类中使用Interface,正在从Recyclerview中删除项目。但是,删除最后一个项目后,它仍在RecyclerView中可见。手动再次调用相同的Fragment后,它将被删除。这是来自Adapter类的代码,用于调用删除方法

 public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView name;
        private  TextView number;
        private  TextView people;
        private TextView myDate;
        private TextView myTime;
        private TextView myNumber;
        private TextView reservationId;
        private TextView foodieId;
        private ImageView deleteReservation;
        private ImageView editReservation;
        public ViewHolder(final View itemView, final OnEntryClickListener listener) {
            super(itemView);
            name=(TextView)itemView.findViewById(R.id.name);
            number=(TextView) itemView.findViewById(R.id.number);
            deleteEntry=(ImageView)itemView.findViewById(R.id.delete);
            editReservation=(ImageView)itemView.findViewById(R.id.editReservation);
            deleteEntry.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.deleteEntry(position);
                        }
                    }
                }
            });        
        }
    }  

这是用于删除显示在Recyclerview类中的项目的代码。

@Override
                                public void deleteEntry(final int position) {
                                    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) {
                                                                                            removeItem(position);
                                        }
                                    });
                                    confirm.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            alertDialog.cancel();
                                        }
                                    });
                                    confirm.show();
                                }  

这是删除项目的方法

public void removeItem(int position) {

                current.remove(position);
                adapter.notifyItemRemoved(position);
                adapter.notifyItemRangeChanged(position, current.size()-1);

            }  

要删除最后一个在删除后也可见的项目,应该做哪些更改?

4 个答案:

答案 0 :(得分:1)

只需将current.size()-1替换为current.size()即可,

//case size == 1
current.remove(position);

//size == 0 here
adapter.notifyItemRemoved(position);

//just call current.size()
adapter.notifyItemRangeChanged(position, current.size());

答案 1 :(得分:0)

还可以这样从数组列表中删除项目

 public void deleteEntry(final int position) {
    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) {
            current.remove(position);
            yourAdapter.notifyDataSetChanged();

        }
    });
    confirm.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            alertDialog.cancel();
        }
    });
    confirm.show();
}

答案 2 :(得分:0)

                               public void deleteEntry(final int position) {
                                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) {
                                      current.remove(position);
                                      notifyDataSetChanged();

                                    }
                                });
                                confirm.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        alertDialog.cancel();
                                    }
                                });
                                confirm.show();
                            }  

答案 3 :(得分:0)

使用它;

        int position = myMatches.indexOf(r);
        if (position > -1) {
            your_list.remove(position);

            notifyItemRemoved(position);
        }