当我滚动它时,我正在使用 GridView,收藏按钮的状态更改为默认状态



我正在使用gridview来显示图像和文本,就像一个简单的电子商务应用程序一样具有收藏夹图标,当我滚动视图时,我标记为收藏夹的项目将更改为其默认状态,即(UN标记(,虽然,就像RecyclerView一样,但我没有使用它,所以找不到任何线索,为什么视图正在变化,感谢带有适当解释的答案。

适配器类

public class Category_Adapter extends BaseAdapter {
private Context mContext;
String name[], img[];
int price[];
LayoutInflater inflater;
public Category_Adapter(Context context, String[] name, String[] img, int price[]) {
this.mContext = context;
this.price = price;
this.name = name;
this.img = img;
inflater = (LayoutInflater) context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
Category_Adapter(Context context) {
this.mContext = context;
}
@Override
public int getCount() {
return name.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public class Holderr {
TextView product_name;
Button addCart;
TextView price_pro;
ImageView product_img, mark, marked;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Holderr holderr = new Holderr();
View rowView;
rowView = inflater.inflate(R.layout.product_category_grid, null);
holderr.product_img = (ImageView
) rowView.findViewById(R.id.imageofitem);
holderr.addCart = rowView.findViewById(R.id.cartmein);
holderr.mark = rowView.findViewById(R.id.markFav);
holderr.marked = rowView.findViewById(R.id.markedFav);
holderr.product_name = (TextView) rowView.findViewById(R.id.productkanam);
holderr.price_pro = (TextView) rowView.findViewById(R.id.priceof_item);
holderr.price_pro.setText("Rs. " + price[position]);
holderr.product_name.setText(name[position]);
Glide.with(mContext).load(img[position]).into(holderr.product_img);
holderr.addCart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toasty.success(mContext, name[position] + " Added in cart", Toast.LENGTH_SHORT).show();
}
});
holderr.mark.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holderr.mark.setVisibility(View.GONE);
holderr.marked.setVisibility(View.VISIBLE);
}
});
holderr.marked.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holderr.marked.setVisibility(View.GONE);
holderr.mark.setVisibility(View.VISIBLE);
}
});
return rowView;
}
}

我在网格视图中设置适配器的类

public class ViewMoreCategory extends AppCompatActivity {
GridView gridView;
ImageButton back, filter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_more_category);
filter = findViewById(R.id.filter_items);
gridView = findViewById(R.id.product_cat_grid);
String name[] = {"Shampoo", "Oil", "Ghee", "Cream", "Shampoo", "Oil", "Ghee", "Cream", "Shampoo", "Oil"};
String img[] = {"http://pngimg.com/uploads/shampoo/shampoo_PNG18.png", "http://pngimg.com/uploads/shampoo/shampoo_PNG18.png", "http://pngimg.com/uploads/shampoo/shampoo_PNG18.png", "http://pngimg.com/uploads/shampoo/shampoo_PNG18.png", "http://pngimg.com/uploads/shampoo/shampoo_PNG18.png", "http://pngimg.com/uploads/shampoo/shampoo_PNG18.png", "http://pngimg.com/uploads/shampoo/shampoo_PNG18.png", "http://pngimg.com/uploads/shampoo/shampoo_PNG18.png", "http://pngimg.com/uploads/shampoo/shampoo_PNG18.png", "http://pngimg.com/uploads/shampoo/shampoo_PNG18.png"};
int price[] = {500, 200, 500, 100, 50, 330, 200, 300, 900, 400};
gridView.setAdapter(new Category_Adapter(ViewMoreCategory.this, name, img, price));
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(R.layout.custom_toolbar);
View view = getSupportActionBar().getCustomView();
back = view.findViewById(R.id.backtoHome);
gridView.setAdapter(new Category_Adapter(this, name, img, price));
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(ViewMoreCategory.this, Dashboard.class));
}
});
}
}

我认为您需要保持所选项目的位置

  1. 将属性添加到Model class,如下所示:
    private int state;
  2. 获取所选项目的位置,然后更改state属性的值
  3. 最后,在您需要Adapter广告中更改imageViewgetView图片

希望对您有所帮助

最新更新