Android卡视图随机文本来自其他卡值的视图
在Android卡视图中,当我首先打开分类页面时,一切都很好,但当我滚动到底部并再次滚动到顶部时,其他卡的值会变成上面一些卡的隐藏值。我需要帮助 下面是产品列表的片段文件 片段文件: 公共类ProductsList扩展了片段{Android卡视图随机文本来自其他卡值的视图,android,cardview,Android,Cardview,在Android卡视图中,当我首先打开分类页面时,一切都很好,但当我滚动到底部并再次滚动到顶部时,其他卡的值会变成上面一些卡的隐藏值。我需要帮助 下面是产品列表的片段文件 片段文件: 公共类ProductsList扩展了片段{ View view; @BindView(R.id.categoryRecyclerView) RecyclerView productsRecyclerView; public static int categoryPosition; @BindView(R.id.n
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();
}
}
公共类ProductListAdapter扩展了RecyclerView.Adapter{
语境;
列出产品清单;
int分类位置;
字符串mrp;
字符串售价;
public ProductListAdapter(上下文上下文、列表productList、int categoryPosition){
this.context=上下文;
this.productList=productList;
this.categoryPosition=categoryPosition;
}
@凌驾
公共HomeProductsViewHolder onCreateViewHolder(视图组父级,int-viewType){
视图=布局更平坦。从(上下文)。充气(R.layout.home\u products\u list\u items,空);
HomeProductsViewHolder HomeProductsViewHolder=新的HomeProductsViewHolder(上下文、视图、产品列表);
返回主页产品查看文件夹;
}
@凌驾
BindViewHolder上的公共无效(最终HomeProductsViewHolder,最终int位置){
holder.cardView.setVisibility(视图.可见);
holder.cardwiew1.setVisibility(View.GONE);
holder.productName.setText(productList.get(position.getProductName());
holder.price.setText(MainActivity.currency+“”+productList.get(position.getSellprice());
试一试{
毕加索。与(上下文)
.load(productList.get(位置).getImages().get(0))
.resize(Integer.parseInt(context.getResources().getString(R.string.targetProductImageWidth)),Integer.parseInt(context.getResources().getString(R.string.targetProductImageHeight)))
.占位符(R.drawable.defaultimage)
.插入(支架图像);
}捕获(例外e){
}
试一试{
double discountPercentage=Integer.parseInt(productList.get(position).getMrpprice())-Integer.parseInt(productList.get(position).getSellprice());
Log.d(“百分比”,折扣百分比+”);
折扣百分比=(折扣百分比/Integer.parseInt(productList.get(position.getMrpprice())*100;
如果((整数)数学四舍五入(折扣百分比)>0){
holder.discountPercentage.setText(((int)数学四舍五入(discountPercentage)+%Off”);
}否则{
持票人.贴现百分比.SETEXT(“”);
}
mrp=productList.get(position.getMrpprice();
sellPrice=productList.get(position.getSellprice();
Log.i(“MRP价格------->>”,MRP+a);
Log.i(“售价->>”,售价+“b”);
holder.actualPrice.setText(MainActivity.currency+“”+productList.get(position.getMrpprice());
holder.actualPrice.setPaintFlags(holder.actualPrice.getPaintFlags()| Paint.STRIKE_-THRU_-TEXT_-FLAG);
}捕获(例外e){
}
holder.itemView.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
ProductDetail.productList.clear();
ProductDetail.productList.addAll(productList);
ProductDetail ProductDetail=新的ProductDetail();
Bundle=新Bundle();
bundle.putInt(“位置”,位置);
productDetail.setArguments(bundle);
((MainActivity)context.loadFragment(productDetail,true);
}
});
}
@凌驾
public int getItemCount(){
返回productList.size();
}
}
RecyclerView
之所以这样称呼,是因为当您滚动时,它的视图被重用。因此,您必须始终将所有属性设置为每个网格单元中的所有视图。例如,您必须将一个空的字符串(“”)作为文本分配给持有者。如果百分比值为RecyclerView
,则会这样调用折扣百分比,因为滚动时会重复使用它的视图。因此,您必须始终将所有属性设置为每个网格单元中的所有视图。例如,您必须将空的字符串(“”)作为文本分配给持有者。如果百分比值为
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);