单击recyclerview适配器中的第一个项目时,删除最后一个项目

时间:2018-10-09 18:35:35

标签: android android-recyclerview

我有一个对象ArrayList的RecyclerView.inflated。我在项目上添加了删除按钮,我想删除单击的项目。但是当单击而不是单击项目时,最后一个项目将被删除。无论单击哪个项目,总是删除最后一个项目。有人知道为什么会这样吗?

适配器:

    public class NotificationAdapter extends RecyclerView.Adapter<NotificationAdapter.NotificationViewHolder> {

    private ArrayList<NotificationItem> mNotificationList;
    private onItemClickListner mListner;

    private Context myContext;
    private int layoutResID;

   public interface onItemClickListner{
     void onItemClick(int position);
     void onDeleteClick(int position);
    }

//    public void setOnItemClickListner(onItemClickListner listner){
//       mListner=listner;
//    }

    //NOTIFICATION HOLDER
    public static class NotificationViewHolder extends RecyclerView.ViewHolder{
        public TextView mNotificationTextView;
        public RelativeLayout mNotificaionHolderLayout;
        public ImageView imageDelete;
        onItemClickListner listner;


        public NotificationViewHolder(View itemView,final onItemClickListner listner) {
            super(itemView);
            mNotificationTextView=itemView.findViewById(R.id.NotificationTextView);
            mNotificaionHolderLayout=itemView.findViewById(R.id.notification__item_container);
            imageDelete=itemView.findViewById(R.id.notification_delete_image);
            this.listner=listner;


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

            imageDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listner!=null){
                        int position=getAdapterPosition();
                        if (position!=RecyclerView.NO_POSITION){
                            listner.onDeleteClick(position);
                        }
                    }
                }
            });
        }
    }//NOTIFICATION HOLDER

    public NotificationAdapter(Context context, int resource, ArrayList<NotificationItem> notificationList,onItemClickListner listner){
        myContext=context;
        this.layoutResID=resource;
        mNotificationList=notificationList;
        this.mListner=listner;
    }


    @Override
    public NotificationViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v=LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_notificationitem,parent,false);
        NotificationViewHolder evh=new NotificationViewHolder(v,mListner);
        return evh;
    }

    @Override
    public void onBindViewHolder(NotificationViewHolder holder, int position) {
        NotificationItem currentItem=mNotificationList.get(position);
        holder.mNotificaionHolderLayout.setBackgroundColor(Color.parseColor(Getcolor(position)));
        holder.mNotificationTextView.setText(currentItem.getNotifi_Name());
    }

    @Override
    public int getItemCount() {
        return mNotificationList.size();
    }

    private String Getcolor(int position)
    {
        String clr;

        switch (position)
        {
            case 0:
                clr="#FF9966";
                break;

            case 1:
                clr="#009900";
                break;

            case 2:
                clr="#006699";
                break;

            case 3:
                clr="#751947";
                break;
            case 4:
                clr="#FF9966";
                break;

            case 5:
                clr="#009900";
                break;

            case 6:
                clr="#006699";
                break;

            case 7:
                clr="#751947";
                break;


            default:
                clr="#FFA500";
                break;
        }
        return clr;
    }

}

家庭活动:

  

我已经在home类中实现了NotificationAdapter并覆盖了这些功能。并将适配器设置为回收站视图。

    @Override
public void onItemClick(int position) {
    FlashMessage("SOMETHING");
    nfAdapter.notifyDataSetChanged();
    nfAdapter.notifyItemRemoved(position);
    nfAdapter.notifyItemRangeChanged(position,listNotifi.size());
}

@Override
public void onDeleteClick(int position) {
       FlashMessage("DELETED ON POSITION : " +position);
       listNotifi.remove(position);
       nfAdapter.notifyDataSetChanged();
       nfAdapter.notifyItemRemoved(position);
       nfAdapter.notifyItemRangeChanged(position,listNotifi.size());

}

1 个答案:

答案 0 :(得分:0)

由于将RecyclerView Adapter设置为在屏幕外重用视图,因此在ViewHolder声明中声明侦听器是一种不好的做法。

如果onClickListeners依赖于项目在整个列表中的位置,那么您将想知道该位置,而不是适配器列表本身中的位置。 (这就是适配器的要点,它是将其视图重用于内存。最好只保留Holder类以将公共变量声明为xml视图id。)只需移动以下行即可:

ngOnInit() {
    this.specialtyQuery = this.route.snapshot.queryParamMap.get('specialty');
    this.languageQuery = this.route.snapshot.queryParamMap.get('language');
    this.genderQuery = this.route.snapshot.queryParamMap.get('gender');
    this.distanceQuery = this.route.snapshot.queryParamMap.get('distance');
    this.affiliationQuery = this.route.snapshot.queryParamMap.get('affiliation');
    this.primaryCareQuery = this.route.snapshot.queryParamMap.get('primaryCare');
    this.accomodationsQuery = this.route.snapshot.queryParamMap.get('accomodations');
    this.newPatientsQuery = this.route.snapshot.queryParamMap.get('newPatients');

    this.providerForm = this.fb.group({
        specialty: this.specialtyQuery || null,
        language: this.languageQuery || null,
        gender: this.genderQuery || null,
        distance: this.distanceQuery || null,
        affiliation: this.affiliationQuery || null,
        primaryCare: this.primaryCareQuery || '',
        accomodations: this.accomodationsQuery || '',
        newPatients: this.newPatientsQuery || ''
    });

}

到您的onBindViewHolder,如下所示:

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

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