Android 卡片视图随机文本来自其他卡片值的视图

Android Card view random text comes in the view from other card values

在 Android 卡片视图中,当我首先打开类别页面时,一切都很好,但是当我滚动到底部并再次滚动到顶部时,其他卡片的值变成了上面某些卡片的隐藏值牌。需要帮助..

以下是产品列表的片段文件

片段文件:

public class ProductsList 扩展片段 {

View view;

@BindView(R.id.categoryRecyclerView)
RecyclerView productsRecyclerView;
public static int categoryPosition;
@BindView(R.id.noProductAddedLayout)
LinearLayout noProductAddedLayout;
@BindView(R.id.contShopping)
Button contShopping;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.fragment_category_list, container, false);
    ButterKnife.bind(this, view);
    MainActivity.title.setText(SplashScreen.categoryListResponseData.get(categoryPosition).getCategory_name());
    setProductsData();
    contShopping.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ((MainActivity) getActivity()).removeCurrentFragmentAndMoveBack();
        }
    });
    return view;
}

@Override
public void onStart() {
    super.onStart();
    ((MainActivity) getActivity()).lockUnlockDrawer(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
    MainActivity.search.setVisibility(View.VISIBLE);
    Config.getCartList(getActivity(), true);

}

@Override
public void onDestroyView() {
    super.onDestroyView();
    MainActivity.search.setVisibility(View.GONE);
}

private void setProductsData() {
    if (SplashScreen.categoryListResponseData.get(categoryPosition).getProducts().size() > 0) {
        ProductListAdapter productListAdapter;
        GridLayoutManager gridLayoutManager;
        gridLayoutManager = new GridLayoutManager(getActivity(), 2);
        productsRecyclerView.setLayoutManager(gridLayoutManager);
        productListAdapter = new ProductListAdapter(getActivity(), SplashScreen.categoryListResponseData.get(categoryPosition).getProducts(), categoryPosition);
        productsRecyclerView.setAdapter(productListAdapter);
    } else {
        noProductAddedLayout.setVisibility(View.VISIBLE);
    }

}

}

适配器文件:

 public class ProductListAdapter extends RecyclerView.Adapter<HomeProductsViewHolder> {
    Context context;
    List<Product> productList;
    int categoryPosition;
    String mrp;
    String sellPrice;
    public ProductListAdapter(Context context, List<Product> productList, int categoryPosition) {
        this.context = context;
        this.productList = productList;
        this.categoryPosition = categoryPosition;
    }

    @Override
    public HomeProductsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.home_products_list_items, null);
        HomeProductsViewHolder homeProductsViewHolder = new HomeProductsViewHolder(context, view, productList);
        return homeProductsViewHolder;
    }

    @Override
    public void onBindViewHolder(final HomeProductsViewHolder holder, final int position) {


        holder.cardView.setVisibility(View.VISIBLE);
        holder.cardView1.setVisibility(View.GONE);
        holder.productName.setText(productList.get(position).getProductName());
        holder.price.setText(MainActivity.currency + " " + productList.get(position).getSellprice());
        try {
            Picasso.with(context)
                    .load(productList.get(position).getImages().get(0))
                    .resize(Integer.parseInt(context.getResources().getString(R.string.targetProductImageWidth)), Integer.parseInt(context.getResources().getString(R.string.targetProductImageHeight)))
                    .placeholder(R.drawable.defaultimage)
                    .into(holder.image);
        } catch (Exception e) {
        }
        try {
            double discountPercentage = Integer.parseInt(productList.get(position).getMrpprice()) - Integer.parseInt(productList.get(position).getSellprice());
            Log.d("percentage", discountPercentage + "");
            discountPercentage = (discountPercentage / Integer.parseInt(productList.get(position).getMrpprice())) * 100;
            if ((int) Math.round(discountPercentage) > 0) {
                holder.discountPercentage.setText(((int) Math.round(discountPercentage) + "% Off"));
            } else {
                holder.discountPercentage.setText("");
            }
            mrp = productList.get(position).getMrpprice();
            sellPrice = productList.get(position).getSellprice();
            Log.i("MRP PRICE------>>>>", mrp +" a");
            Log.i("Sell Price PRICE->>", sellPrice +" b");
            holder.actualPrice.setText(MainActivity.currency + " " + productList.get(position).getMrpprice());
            holder.actualPrice.setPaintFlags(holder.actualPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

        } catch (Exception e) {
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ProductDetail.productList.clear();
                ProductDetail.productList.addAll(productList);
                ProductDetail productDetail = new ProductDetail();
                Bundle bundle = new Bundle();
                bundle.putInt("position", position);
                productDetail.setArguments(bundle);
                ((MainActivity) context).loadFragment(productDetail, true);
            }
        });


    }

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

}

RecyclerView 之所以这样调用,是因为它的 View 在您滚动时会被重复使用。因此,您始终必须将所有属性设置为每个网格单元格中的所有 View。例如,如果百分比值 <= 0,则必须将空的 String ("") 作为文本分配给 holder.discountPercentage

另一件事:您需要 try-catch 块,因为您想要计算 double discountPercentage 的值。知道此值后,您可以在 try-catch 块之外继续。

double discountPercentage = 0;
try {
    double discountPercentage = Integer.parseInt(productList.get(position).getMrpprice()) - Integer.parseInt(productList.get(position).getSellprice());
    Log.d("percentage", discountPercentage + "");
    discountPercentage = (discountPercentage / Integer.parseInt(productList.get(position).getMrpprice())) * 100;
}
catch (Exception e) {
    // Log exception here 
}
finally{
    // either you managed to calculate a value for discountPercentage
    // or it is still zero
    // Anyway, you can set the text for the TextView now:
    if ((int) Math.round(discountPercentage) > 0) {
        holder.discountPercentage.setText(((int) Math.round(discountPercentage) + "% Off"));
    }
    // Note: there always has to be an "else" block, 
    // or the recycled View will keep the content from 
    // the previous call to onBindViewHolder()
    else {
         holder.discountPercentage.setText("");
    }
}
// as far as I can see, the TextView for the actual price
// does not depend on the discountPercentage
// so it can stay outside of the try-catch block
holder.actualPrice.setText(MainActivity.currency + " " + productList.get(position).getMrpprice());
holder.actualPrice.setPaintFlags(holder.actualPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);