RecyclerView элементы мерцают при обновлении элемента - PullRequest
0 голосов
/ 10 октября 2019

Я использую RecyclerView для хранения моего списка предметов. Каждый элемент имеет название продукта, штрих-код и кнопку плюс / минус для обновления количества. Когда я нажимаю на плюс / минус любого элемента, мой список мерцает (прокручивается), и я получаю другой элемент для обновления, после которого по ошибке будет изменено количество другого элемента. Я перепробовал все, но ничего не работает нормально. Я хочу обновить любой другой элемент, но это приводит к обновлению 2-го элемента. Что делать дальше.

Код адаптера

public class AdapterEndStock extends RecyclerView.Adapter<AdapterEndStock.MyViewHolder> {


Context context;
EndStockFragment endStockFragment;
ArrayList<ModelEndStock> endStockArrayList;
DatabaseHelper databaseHelper;
ModelEndStock estimationData;
ArrayList<Product> productDataArrayList;
ArrayList<Product> subBoxArrayList;
ArrayList<Product> singleUnitArrayList;


int qty;


public AdapterEndStock(Context context, ArrayList<ModelEndStock> endStockArrayList, EndStockFragment endStockFragment) {
    this.context = context;
    this.endStockArrayList = endStockArrayList;
    this.endStockFragment = endStockFragment;
}


@NonNull
@Override
public AdapterEndStock.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    final View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_end_stock, parent, false);


    return new AdapterEndStock.MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull final AdapterEndStock.MyViewHolder holder, final int position) {
    databaseHelper = new DatabaseHelper(context);

    estimationData = endStockArrayList.get(position);
    holder.txtProductName.setText(estimationData.getProduct_name());
    holder.txtCodeValue.setText(estimationData.getProduct_barcode());
    holder.txtNetStockValue.setText(estimationData.getProduct_netStock());
    productDataArrayList = new ArrayList<>();
    subBoxArrayList = new ArrayList<>();
    singleUnitArrayList = new ArrayList<>();


    if (estimationData.getProduct_endStock() == (null)) {
        holder.etQty.setText("0");

    } else {
        holder.etQty.setText(String.valueOf(estimationData.getProduct_endStock()));
        //holder.txtDelieveredValue.setText(String.valueOf(estimationData.getProduct_ordered()));
    }


    Log.e("tag", "net_stock" + estimationData.getProduct_netStock() + estimationData.getProduct_type() + estimationData.getProduct_barcode());


    productDataArrayList = databaseHelper.getProductByEANCode(estimationData.getProduct_barcode());
    subBoxArrayList = databaseHelper.getSubBoxByCode(estimationData.getProduct_barcode());
    singleUnitArrayList = databaseHelper.getSingleUnitByCode(estimationData.getProduct_barcode());


    if (productDataArrayList.size() != 0) {
        holder.imgPType.setImageResource(R.drawable.black_box);
    } else if (subBoxArrayList.size() != 0) {
        holder.imgPType.setImageResource(R.drawable.black_box);
    } else if (singleUnitArrayList.size() != 0) {
        holder.imgPType.setImageResource(R.drawable.wine_icon_black);
    }


    holder.ibPlus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            ModelEndStock estimationData = endStockArrayList.get(position);

            qty = Integer.parseInt(holder.etQty.getText().toString());
            qty = qty + 1;

            holder.etQty.setText(qty + "");


            endStockFragment.updateEndProduct(estimationData.getProduct_id(), estimationData.getProduct_name(),
                    estimationData.getProduct_barcode(), estimationData.getProduct_type(), estimationData.getProduct_netStock(), String.valueOf(qty));

String.valueOf(qty),estimationData.getProductType());


        }
    });

    holder.ibMinus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            ModelEndStock estimationData = endStockArrayList.get(position);


            qty = Integer.parseInt(holder.etQty.getText().toString());
            if (qty > 0) {
                qty = qty - 1;
                holder.etQty.setText(qty + "");

                endStockFragment.updateEndProduct(estimationData.getProduct_id(), estimationData.getProduct_name(),
                        estimationData.getProduct_barcode(), estimationData.getProduct_type(), estimationData.getProduct_netStock(), String.valueOf(qty));


            }


        }
    });

    holder.ibDelete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            AlertDialog.Builder alertDialog = new AlertDialog.Builder(context, R.style.yourDialog);
            alertDialog.setMessage(R.string.delete_product);
            alertDialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    endStockFragment.deleteProduct(position);

                }
            });
            alertDialog.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    dialog.cancel();
                }
            });
            alertDialog.show();


        }
    });
}


private static class ViewHolder {


}

public class MyViewHolder extends RecyclerView.ViewHolder {

    TextView txtProductName, txtCodeValue, txtNetStockValue;
    ImageButton ibMinus, ibPlus, ibDelete;
    EditText etQty;
    ImageView imgPType;

    public MyViewHolder(View itemView) {
        super(itemView);


        txtProductName = (TextView) itemView.findViewById(R.id.txtProductName);
        txtCodeValue = (TextView) itemView.findViewById(R.id.txtCodeValue);

        txtNetStockValue = (TextView) itemView.findViewById(R.id.txtNetStockValue);
        ibPlus = (ImageButton) itemView.findViewById(R.id.ibPlus);
        ibMinus = (ImageButton) itemView.findViewById(R.id.ibMinus);

        ibDelete = (ImageButton) itemView.findViewById(R.id.ibDelete);
        etQty = (EditText) itemView.findViewById(R.id.etQty);
        imgPType = (ImageView) itemView.findViewById(R.id.imgPType);
    }
}

@Override
public long getItemId(int position) {

    ModelEndStock modelEndStock = endStockArrayList.get(position);
    return Integer.parseInt(modelEndStock.getProduct_id());
}

@Override
public int getItemViewType(int position) {
    return position;
}

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


}

Код рециркуляции Просмотр:

AdapterEndStock adapter = new AdapterEndStock(getActivity(), getModelEndArrayList, EndStockFragment.this);
        RecyclerView.LayoutManager LayoutManager = new LinearLayoutManager(getActivity());
        list.getItemAnimator().endAnimations();
        list.setLayoutManager(LayoutManager);
        adapter.setHasStableIds(true);
        list.setAdapter(adapter);
        adapter.notifyDataSetChanged();

Обновить код количества товара

 public void updateEndProduct(String product_id, String product_name, String product_barcode, String product_type,
                             String product_ordered, String product_returned) {


    ModelEndStock updateEnd = new ModelEndStock();

    updateEnd.setProduct_id(product_id);
    updateEnd.setProduct_name(product_name);
    updateEnd.setProduct_barcode(product_barcode);
    updateEnd.setProduct_netStock(product_ordered);
    updateEnd.setProduct_type(product_type);
    updateEnd.setProduct_endStock(product_returned);

    Log.e("TAG", "updateEndProduct: "+product_returned );


    dbhelper.addEndProductCart(updateEnd);

    ArrayList<ModelEndStock > m1 = new ArrayList<>();
    m1 = dbhelper.getEndCart("");

    for(ModelEndStock modelEndStock : m1){

        Log.e("TAG", "updateGEndProduct: "+modelEndStock.getProduct_barcode() +"**"+modelEndStock.getProduct_name() +"^^"+modelEndStock.getProduct_endStock() );
    }


}
...