我正在我的应用中实现购物车。产品使用RecyclerView
显示,用户输入数量EditText
。
看起来像这样:
截屏
问题:
现在我有13个产品。假设我输入数量为1.0,2.5,3.5,就产品2,3和4而言,然后我向下滚动到最后并返回到屏幕的开头,数量设置为1.0,2.5,3.5,1.0,2.5和产品2,3,4,11,12,13分别为3.5。
我已经搜索了很多关于SO的类似问题,但到目前为止没有任何帮助。请帮忙。
我的代码是:
package com.organicbasket.retailer_app.adapter;
import android.content.Context;
import android.graphics.Point;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.organicbasket.retailer_app.R;
import com.organicbasket.retailer_app.fragment.ProductListFragment;
import com.organicbasket.retailer_app.model.Product;
import com.organicbasket.retailer_app.society.ShoppingActivity;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import static android.text.TextUtils.isEmpty;
public class ProductListAdapter extends RecyclerView.Adapter<ProductListAdapter.ProductViewHolder> {
private ShoppingActivity shoppingActivity;
private final Context context;
private ProductListFragment productListFragment;
private ArrayList<Product> productItems;
public ProductListAdapter(ShoppingActivity activity, List<Product> productItems, ProductListFragment productListFragment) {
this.context = activity.getApplicationContext();
this.shoppingActivity = activity;
this.productListFragment = productListFragment;
this.productItems = new ArrayList<>();
this.productItems.addAll(productItems);
}
public class ProductViewHolder extends RecyclerView.ViewHolder{
ImageView thumbNail;
TextView name;
TextView rate;
EditText etQuantity;
TextView remarks;
Button addToCart;
public ProductViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.product_name);
rate = (TextView) itemView.findViewById(R.id.rate);
etQuantity = (EditText) itemView.findViewById(R.id.quantity);
remarks = (TextView) itemView.findViewById(R.id.remarks);
thumbNail = (ImageView) itemView.findViewById(R.id.thumbnail);
addToCart = (Button) itemView.findViewById(R.id.addtocart);
}
}
@Override
public ProductViewHolder onCreateViewHolder(ViewGroup viewGroup, final int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.productlist_row, viewGroup, false);
ProductViewHolder viewHolder = new ProductViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(final ProductViewHolder viewHolder, final int position) {
Product currentProduct;
currentProduct = productItems.get(position);
viewHolder.etQuantity.setText(Double.toString(productItems.get(position).getItemQuantity()));
viewHolder.name.setText(currentProduct.getName());
NumberFormat nf = new DecimalFormat("##.###");
viewHolder.rate.setText("Rs " + String.valueOf(currentProduct.getRate()) + " per " + nf.format(currentProduct.getQuantity()) + " " + String.valueOf(currentProduct.getUnit()));
viewHolder.remarks.setText(String.valueOf(currentProduct.getRemarks()));
viewHolder.etQuantity.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
double value = 0.0;
if(shoppingActivity.productList.size() <= 0){
return;
}
if (isEmpty(s.toString())){
for (int i = 0; i < shoppingActivity.myCart.productList.size(); i++) {
if (Integer.toString(productItems.get(position).getID()).equals(Integer.toString(shoppingActivity.myCart.productList.get(i).getID()))) {
shoppingActivity.myCart.productList.remove(i);
break;
}
}
for (int i = 0; i < shoppingActivity.productList.size(); i++) {
if (Integer.toString(productItems.get(position).getID()).equals(Integer.toString(shoppingActivity.productList.get(i).getID()))) {
shoppingActivity.productList.get(i).setItemQuantity(0);
break;
}
}
viewHolder.addToCart.setVisibility(View.GONE);
Toast.makeText(shoppingActivity, "Removed from Cart", Toast.LENGTH_SHORT).show();
return;
}
try {
value = Double.parseDouble(s.toString());
if(value <= 0) {
viewHolder.addToCart.setVisibility(View.GONE);
return;
}
if (viewHolder.addToCart.getVisibility() == View.VISIBLE) {
for (int i = 0; i < shoppingActivity.productList.size(); i++) {
if (Integer.toString(productItems.get(position).getID()).equals(Integer.toString(shoppingActivity.productList.get(i).getID()))) {
shoppingActivity.productList.get(i).setItemQuantity(value);
break;
}
}
for (int i = 0; i < shoppingActivity.myCart.productList.size(); i++) {
if (Integer.toString(productItems.get(position).getID()).equals(Integer.toString(shoppingActivity.myCart.productList.get(i).getID()))) {
shoppingActivity.myCart.productList.get(i).setItemQuantity(value);
break;
}
}
} else {
shoppingActivity.myCart.productList.add(productItems.get(position));
for (int i = 0; i < shoppingActivity.productList.size(); i++) {
if (Integer.toString(productItems.get(position).getID()).equals(Integer.toString(shoppingActivity.productList.get(i).getID()))) {
shoppingActivity.productList.get(i).setItemQuantity(value);
break;
}
}
viewHolder.addToCart.setVisibility(View.VISIBLE);
viewHolder.addToCart.setText("REMOVE");
viewHolder.addToCart.setBackground(shoppingActivity.getResources().getDrawable(R.drawable.round_button_purple));
viewHolder.addToCart.setTextColor(shoppingActivity.getResources().getColor(R.color.white));
}
}
catch (Exception e){
}
}
}
);
}
@Override
public int getItemCount() {
return productItems.size();
}
@Override
public long getItemId(int position) {
return position;
}
}
答案 0 :(得分:2)
试试这个: -
只需在viewHolder.setIsRecyclable(false);
onBindViewHolder
即可
答案 1 :(得分:0)
只需在适配器类中添加以下代码行即可实现结果,
@Override
public int getItemViewType(int position) {
return position;
}