如何将侦听器添加到视图持有者以防止它在不同条件下被回收

how to add a listener to a view holder to prevent it from gets recycling under different conditions

我正在处理一家理发店的申请,我有一份订单清单 其中一些是针对成人的,另一些是针对儿童的,所以当用户为一个人选择一项服务并按添加时,我会显示价格,但每当列表增长并且我从中间删除一些人时,所有随后的价格都会恢复为零 我认为那是因为我以零开始持有人,但我想要的是每当我删除一个项目时,所有其他项目都不会被回收

ViewHolder

public class OrderDetailViewHolder extends RecyclerView.ViewHolder    {
    private static final String TAG = "OrderDetailViewHolder";
    public Button offersBtn,servicesBtn;
    public TextView priceTxt,personTxt;
    IServer server;
    Context mContext;
ChainedCallbackForHairdresserTotal chainedCallbackForHairdresserTotal;

    public void setChainedCallbackForHairdresserTotal(ChainedCallbackForHairdresserTotal chainedCallbackForHairdresserTotal) {
        this.chainedCallbackForHairdresserTotal = chainedCallbackForHairdresserTotal;
    }

    ArrayList<String>currentServices=new ArrayList<>();
    HashMap<String,String> currentOffers=new HashMap<>();

    ArrayList<Models>servicesAdult=new ArrayList<>();
    ArrayList<Models>servicesChild=new ArrayList<>();
    ArrayList<Models>offersAdult=new ArrayList<>();
    ArrayList<Models> offersChild=new ArrayList<>();
    ArrayList<Models>DialogArray=new ArrayList<>();

        public OrderDetailViewHolder(@NonNull View itemView, final Context mContext) {
        super(itemView);
        offersBtn=itemView.findViewById(R.id.offersBtn);
        servicesBtn=itemView.findViewById(R.id.servicesBtn);
        priceTxt=itemView.findViewById(R.id.priceTxt);
        personTxt=itemView.findViewById(R.id.personTxt);
        this.mContext=mContext;
        Fonts fonts=new Fonts(mContext);
        fonts.setTypeFce(itemView);
        server=Common.getAPI();

loadServices();



        Log.d(TAG, "OrderDetailViewHolder: ");

        }

    private void loadServices() {
        Log.d(TAG, "loadServices: currentSaloon.getSaloonId() "+Common.currentSaloon.getSaloonId());
        server.getServices(String.valueOf(Common.currentSaloon.getSaloonId())).enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                Log.d(TAG, "onResponse: "+response.body());
                Parser parser=new Parser(mContext);
                parser.parse(response.body());
                Log.d(TAG, "onResponse: "+parser.getServices().size());
                getSaloonServicesAndOffers(parser.getServices());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Log.d(TAG, "onFailure: "+t.getMessage());
            }
        });
    }



    public void setListeners(final Models orderDetailModel){
        servicesBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // show Services Dialog

                String orderType=orderDetailModel.getOrderPersonType();
                if(orderType.equals("child")){
                    DialogArray=servicesChild;
                }
                else if(orderType.equals("adult")){
                    DialogArray=servicesAdult;
                }
                ListsDialog listsDialog=new ListsDialog(mContext,Common.DIALOG_LAYOUT_TYPE_SERVICE,getAdapterPosition(),DialogArray,null,"",0,currentServices,currentOffers);
                listsDialog.setOnPersonTotalComputed(new onPersonTotalComputed() {
                    @Override
                    public void onPersonTotalComputed(float personTotal) {
                        priceTxt.setText(String.valueOf((int)personTotal)+"EGP");
                  chainedCallbackForHairdresserTotal.getTotal(personTotal,true);
                        if(personTotal==0){
                            priceTxt.setText("0EGP");
                        }
                    }
                });
                listsDialog.show();

            }
        });

        offersBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // show Services Dialog
                String orderType=orderDetailModel.getOrderPersonType();
                if(orderType.equals("child")){
                    DialogArray=offersChild;
                }
                else if(orderType.equals("adult")){
                    DialogArray=offersAdult;
                }
    ListsDialog listsDialog=new ListsDialog(mContext,Common.DIALOG_LAYOUT_TYPE_OFFER,getAdapterPosition(),DialogArray,null,"",0,currentServices,currentOffers);
                listsDialog.setOnPersonTotalComputed(new onPersonTotalComputed() {
                    @Override
                    public void onPersonTotalComputed(float personTotal) {
                        priceTxt.setText(String.valueOf((int)personTotal)+"EGP");
                        chainedCallbackForHairdresserTotal.getTotal(personTotal,true);
                        if(personTotal==0){
                            priceTxt.setText("0EGP");
                        }
                    }
                });

                listsDialog.show();

            }
        });
    }



    public void bind(Models orderDetailModel, int pos) {

            

    personTxt.setText(orderDetailModel.getOrderPersonType().equals("adult")? mContext.getString(R.string.adult)+String.valueOf(orderDetailModel.getOrderId())
            :mContext.getString(R.string.child)+String.valueOf(orderDetailModel.getOrderId()));

    priceTxt.setText("0EGP");
    setListeners(orderDetailModel);




    }

这里是1 when the list only has 3 adults everything is fine and then when I added a child2 also all is fine then I added an adult#4 which has a 40 price but when I removed child#1 the price of adult#4 went back to zero 3如何防止这种情况?

RecyclerView 是违反所有逻辑的。支架必须回收利用。

您可以将获取的所有数据存储在适配器列表中,因为适配器持有它的引用并且您可以从 ViewHolder 访问它。

此外,最佳做法是在 ViewHolder

之外执行所有异步逻辑

答案是每当你删除一个项目时只写 notifyItemRemoved(position); 这不会重新创建列表中的下一个查看器 而不是 notifyItemRangeChanged(position, orders.size());这将重新创建下一个项目