我在包含产品列表的应用中使用RecyclerView。用户可以更改每个项目的数量。但是问题是,当我滚动更改的每种产品的数量时。我检查了我的代码,但工作正常。该怎么办。请帮忙。滚动时会更改项目数量
代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/dp10">
<TextView
android:id="@+id/txt_review_incorrect_delivery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="@dimen/dp10"
android:text="@string/pre_order"
android:textColor="@color/colorPrimary"
android:textSize="@dimen/dp20"
android:textStyle="bold" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/txt_review_incorrect_delivery">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/dp10"
android:orientation="vertical">
<LinearLayout
android:id="@+id/layProducts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="@+id/txtProducts"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.95"
android:background="@drawable/blue_strip"
android:padding="@dimen/dp10"
android:text="@string/products"
android:textColor="@android:color/white"
android:textSize="@dimen/dp18" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/listProducts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp10"
android:divider="@null"
android:dividerHeight="0dp"
tools:listitem="@layout/item_pre_order_article" />
<LinearLayout
android:id="@+id/layTrading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="@+id/txtTrading"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp10"
android:layout_weight="0.95"
android:background="@drawable/blue_strip"
android:padding="@dimen/dp10"
android:text="@string/trading"
android:textColor="@android:color/white"
android:textSize="@dimen/dp18" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/listTrading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp10"
android:divider="@null"
android:dividerHeight="0dp"
tools:listitem="@layout/item_pre_order_article" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="@+id/txtListUnrecognisedCode"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp10"
android:layout_weight="0.95"
android:background="@drawable/blue_strip"
android:padding="@dimen/dp10"
android:text="@string/list_unrecognised"
android:textColor="@android:color/white"
android:textSize="@dimen/dp18" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/listUnrecognisedCode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp10"
android:divider="@null"
android:dividerHeight="0dp"
tools:listitem="@layout/item_list_unrecognised_code" />
<TextView
android:id="@+id/txtRemarksShopOwner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp10"
android:padding="@dimen/dp10"
android:text="@string/remarks_shop_owner"
android:textColor="@color/colorNormalGray"
android:textSize="@dimen/dp18"
android:textStyle="bold" />
<EditText
android:id="@+id/etRemarksShopOwner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/text_box"
android:padding="@dimen/dp5" />
<TextView
android:id="@+id/txtInternalRemarks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp10"
android:padding="@dimen/dp10"
android:text="@string/internal_remarks"
android:textColor="@color/colorNormalGray"
android:textSize="@dimen/dp18"
android:textStyle="bold" />
`
<EditText
android:id="@+id/etInternalRemarks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/text_box"
android:padding="@dimen/dp5" />
<ImageButton
android:id="@+id/btnSubmitReport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/dp20"
android:layout_marginBottom="@dimen/dp10"
android:background="@android:color/transparent"
android:src="@drawable/submit_pre_order" />
</LinearLayout>
</ScrollView>
<ProgressBar
android:id="@+id/pBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone" />
适配器代码:
public class AdapterPreOrderProducts extends RecyclerView.Adapter<AdapterPreOrderProducts.MyViewHolder> {
Context context;
Integer articelQuantity;
ArrayList<ModelCart> modelCartProductList;
ModelCart modelCart;
DatabaseHelper databaseHelper;
FinalizePreOrderFragment finalizePreOrderFragment;
int product_position;
public AdapterPreOrderProducts(Context context, ArrayList<ModelCart> modelCartProductList, FinalizePreOrderFragment finalizePreOrderFragment) {
this.context = context;
this.modelCartProductList = modelCartProductList;
this.finalizePreOrderFragment = finalizePreOrderFragment;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView txtArticleName, txtArticleNo, txtEANNo, txtMBHQty, txtArticleQuantity;
ImageButton imgAddQty, imgSubQty;
ImageView imgDeleteOrder;
EditText etArticleQty;
public MyViewHolder(View view) {
super(view);
txtArticleName = (TextView) view.findViewById(R.id.txtArticleName);
txtArticleNo = (TextView) view.findViewById(R.id.txtArticleNo);
imgAddQty = (ImageButton) view.findViewById(R.id.imgAddQty);
imgSubQty = (ImageButton) view.findViewById(R.id.imgSubQty);
etArticleQty = (EditText) view.findViewById(R.id.etArticleQty);
txtEANNo = (TextView) view.findViewById(R.id.txtEANNo);
txtMBHQty = (TextView) view.findViewById(R.id.txtMBHQty);
txtArticleQuantity = (TextView) view.findViewById(R.id.txtArticleQuantity);
imgDeleteOrder = (ImageView) view.findViewById(R.id.imgDeleteOrder);
}
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_pre_order_article, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
databaseHelper = new DatabaseHelper(context);
modelCart = modelCartProductList.get(position);
holder.txtArticleName.setText(modelCart.getCart_product_name_fr());
holder.txtArticleNo.setText(modelCart.getCart_product_art());
holder.txtEANNo.setText(modelCart.getCart_product_EAN());
holder.txtMBHQty.setText(modelCart.getCart_product_MBH());
holder.txtArticleQuantity.setText(modelCart.getProduct_total_quantity());
holder.etArticleQty.setText(modelCart.getProduct_quantity());
holder.imgDeleteOrder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finalizePreOrderFragment.deleteProduct(position);
}
});
// click listener on buttons
holder.imgAddQty.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ModelCart modelCart1 = modelCartProductList.get(position);
articelQuantity = Integer.parseInt(holder.etArticleQty.getText().toString());
articelQuantity = articelQuantity + 1;
holder.etArticleQty.setText(articelQuantity + "");
int totalQty = articelQuantity * Integer.parseInt(modelCart1.getCart_product_MBH());
Log.e("TAG", "onClick:totalQty" + totalQty);
holder.txtArticleQuantity.setText(String.valueOf(totalQty));
int MBH = Integer.parseInt(modelCart1.getCart_product_MBH());
Log.e("TAG", "onClick:alldetails" + modelCart1.getCart_product_id() + " " + modelCart1.getProduct_quantity());
modelCart1.setProduct_quantity(articelQuantity.toString());
modelCart1.setProduct_total_quantity(MBH * articelQuantity + "");
modelCart1.setCart_product_id(modelCart1.getCart_product_id());
databaseHelper.updateCartTable(modelCart1);
finalizePreOrderFragment.grandTotalProducts(modelCartProductList);
finalizePreOrderFragment.calculateFranco();
}
});
holder.imgSubQty.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (Integer.parseInt(holder.etArticleQty.getText().toString()) > 1) {
ModelCart modelCart1 = modelCartProductList.get(position);
holder.imgSubQty.setClickable(true);
articelQuantity = Integer.parseInt(holder.etArticleQty.getText().toString());
articelQuantity = articelQuantity - 1;
holder.etArticleQty.setText(articelQuantity + "");
int MBH = Integer.parseInt(modelCart1.getCart_product_MBH());
int totalQty = articelQuantity * Integer.parseInt(modelCart1.getCart_product_MBH());
holder.txtArticleQuantity.setText(String.valueOf(totalQty));
modelCart1.setProduct_quantity(articelQuantity.toString());
modelCart1.setProduct_total_quantity(MBH * articelQuantity + "");
modelCart1.setCart_product_id(modelCart1.getCart_product_id());
Log.e("TAG", "onClick:alldetailsminus" + modelCart1.getCart_product_id() + " " + modelCart1.getProduct_quantity());
databaseHelper.updateCartTable(modelCart1);
finalizePreOrderFragment.grandTotalProducts(modelCartProductList);
finalizePreOrderFragment.calculateFranco();
}
}
});
holder.etArticleQty.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if (!(holder.etArticleQty.getText().toString().matches(""))) {
ModelCart modelCart1 = modelCartProductList.get(position);
articelQuantity = Integer.parseInt(holder.etArticleQty.getText().toString());
int MBH = Integer.parseInt(modelCart1.getCart_product_MBH());
int totalQty = articelQuantity * Integer.parseInt(modelCart1.getCart_product_MBH());
holder.txtArticleQuantity.setText(String.valueOf(totalQty));
modelCart1.setProduct_quantity(articelQuantity.toString());
modelCart1.setProduct_total_quantity(MBH * articelQuantity + "");
modelCart1.setCart_product_id(modelCart1.getCart_product_id());
databaseHelper.updateCartTable(modelCart1);
finalizePreOrderFragment.grandTotalProducts(modelCartProductList);
finalizePreOrderFragment.calculateFranco();
}
}
});
}
@Override
public int getItemCount() {
return modelCartProductList.size();
}
}