在 RecyclerView 中搜索项目时图像视图的位置发生变化

Position of an imageview changing when searching for an item in RecyclerView

我有一个 RecyclerView,其中每个项目都有一个 ImageView,形式为 sendEmail 图标。最初,ImageView 是可见的,但一旦我点击它,我就使用 setVisibility(View.GONE) 隐藏它并更新 Adapter.

现在,当我点击 sendEmail 图标时,我会立即使用 reportitems.get(position).setStatus("emailsent"); 隐藏该位置的图标。现在,如果在 search 操作之前,第二个项目有 ImageView 可见而第一个项目没有,那么在搜索之后如果第二个项目是唯一相关的项目,那么 ImageView 没有出现在第一个位置。我正在使用动态搜索,在输入字符时,适配器会立即刷新并显示更新后的 RecyclerView。我该如何解决这个问题?

搜索后,虽然SQ 322应该有sendEmail图标,但没有出现

Activity代码

mAdapter.setOnItemClickListener(new ReportAdapter.OnItemClickListener() {
    public void onSendEmailClick(int position){


        flightNumber=reportitems.get(position).getFlightNumber();
        departureDate=reportitems.get(position).getDepartureDate();

        FlightClosureStatus flightClosureStatus=new FlightClosureStatus(flightNumber,departureDate,"emailsent");
        flightViewModel.updateFlightClosureStatus(flightClosureStatus);

        reportitems.get(position).setStatus("emailsent");
        mAdapter.notifyDataSetChanged();
        }
        }

适配器代码

public class ReportAdapter extends RecyclerView.Adapter<ReportAdapter.ReportViewHolder> {

    private ArrayList<ReportItem> reportlist;
    private OnItemClickListener mListener;
    private Context mContext;

    public ReportAdapter(ArrayList<ReportItem> reportlist, Context context) {
        this.reportlist = reportlist;
        this.mContext = context;
    }

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

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

    public static class ReportViewHolder extends RecyclerView.ViewHolder {

        public TextView departureDate;
        public TextView flightNumber;
        public ImageView emailView;
  
        public ReportViewHolder(@NonNull View itemView, OnItemClickListener listener, Context context) {
            super(itemView);
            departureDate = itemView.findViewById(R.id.departureDaterecyclerview);
            flightNumber =  itemView.findViewById(R.id.flightnumberrecyclerview);
            emailView = itemView.findViewById(R.id.sendemailIcon);
           
            emailView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if(listener != null) {
                        int position = getAdapterPosition();
                        if(position != RecyclerView.NO_POSITION) {
                            listener.onSendEmailClick(position);

                        }
                    }

                }
            });

        }
    }

    @NonNull
    @Override
    public ReportViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.report_listing_item, parent, false);
        ReportViewHolder rvh= new ReportViewHolder(v,mListener,mContext);
        return rvh;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void onBindViewHolder(@NonNull ReportViewHolder holder, int position) {

        ReportItem currentItem = reportlist.get(position);

        //here i am setting the visibility of the imageview to gone
        if(currentItem.getStatus().contentEquals("emailsent")){
            holder.emailView.setVisibility(View.GONE);
        }

        holder.flightNumber.setText(currentItem.getFlightNumber());
        holder.departureDate.setText((currentItem.getDepartureDate()));


    }

    public List<ReportItem> getList() {
        return reportlist;
    }
}

将您的 onBindViewHolder 替换为以下内容,基本上回收商会在可用时使用池中的物品,并且一旦物品可见性为 GONE 并且永远不会再次设置为 VISIBLE 除非您这样做它

 @SuppressLint("ResourceAsColor")
    @Override
    public void onBindViewHolder(@NonNull ReportViewHolder holder, int position) {

        ReportItem currentItem = reportlist.get(position);

        //here i am setting the visibility of the imageview to gone
        if(currentItem.getStatus().contentEquals("emailsent")){
            holder.emailView.setVisibility(View.GONE);
        }else{
            holder.emailView.setVisibility(View.VISIBLE);
        }

        holder.flightNumber.setText(currentItem.getFlightNumber());
        holder.departureDate.setText((currentItem.getDepartureDate()));


    }