【发布时间】:2020-11-05 08:09:48
【问题描述】:
在Android卡片视图中,当我首先打开类别页面时,一切都很好,但是当我滚动到底部并再次滚动到顶部时,其他卡片的值会出现上面一些卡片的隐藏值。需要帮助..
以下是产品列表的片段文件
片段文件:
公共类 ProductsList 扩展 Fragment {
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();
}
}
【问题讨论】: