如何在视图持有者中添加侦听器以防止其在不同条件下回收

时间:2019-07-17 09:10:32

标签: android android-recyclerview android-viewholder

我正在为一家理发店做申请,我有一份订单清单 其中一些是成人的,其他的是儿童的,因此,当用户选择某人的服务并按“添加”时,我会显示价格,但是只要列表增加,我就会从中间移除某个人,其后所有价格都将返回零 我认为那是因为我以零开始的持有人,但我想要的是每当我删除一个项目时,所有其他项目都不会被回收

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,当列表中只有3个成人时,一切都很好,然后当我添加一个孩子2时,一切都很好,然后我添加了一个成人,价格为40,但是当我删除了孩子#1,成人#4的价格又回到了零3,如何防止呢?

2 个答案:

答案 0 :(得分:0)

创建RecyclerView违反所有逻辑。持有人必须被回收。

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

最好的做法是在ViewHolder之外执行所有异步逻辑

答案 1 :(得分:0)

答案是,每当您删除项目时,只需编写notifyItemRemoved(position);即可。 这不会在列表中重新创建下一个视图持有者 而不是notifyItemRangeChanged(position,orders.size());这将重新创建下一个项目