我尝试在我的应用中实现添加到收藏夹功能。我在列表视图中有两个图像按钮ddToFavouritiesBtn
和removeFromFavouritiesBtn
。一次只有一个图像按钮在选择时可见。
我的问题是,当我选择addToFavouritiesBtn
时,它会被选中,但当我向下滚动列表视图并返回到该图像按钮时,它仍然未被选中。
请告诉我如何在滚动时保持所选项目的状态。
这是我的代码。
public class Product{
private boolean selected = false;
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
ProdctListAdaptet.class
public class ProductListAdapter extends BaseAdapter {
private Activity activity;
private List<Product> productItems;
private Context context;
int pos;
int flag = 0 ;
public ProductListAdapter(Context context,Activity activity, List<Product> productItems,int flag) {
this.activity = activity;
this.productItems = productItems;
this.context = context;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// ProductHolder holder = null;
final ProductHolder holder;
if (convertView == null) {
holder = new ProductHolder();
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.product_list_row_item, parent,false);
holder.thumbNail = (ImageView) convertView
.findViewById(R.id.product_image);
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.btnAddFavourite = (ImageButton) convertView.findViewById(R.id.AddFavourites);
holder.btnRemoveFavourite = (ImageButton) convertView.findViewById(R.id.RemoveFavourites);
holder.btnAddFavourite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
flag = 1;
System.out.println("Add favourities");
pos = (Integer) v.getTag();
productItems.get(pos).setSelected(false);
holder.btnAddFavourite.setVisibility(View.GONE);
holder.btnRemoveFavourite.setVisibility(View.VISIBLE);
//notifyDataSetChanged();
}
});
holder.btnRemoveFavourite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
flag = 2;
System.out.println("Remove favorities");
pos = (Integer) v.getTag();
productItems.get(pos).setSelected(true);
holder.btnAddFavourite.setVisibility(View.VISIBLE);
holder.btnRemoveFavourite.setVisibility(View.GONE);
// notifyDataSetChanged();
}
});
convertView.setTag(holder);
} else {
holder = (ProductHolder) convertView.getTag();
}
holder.btnAddFavourite.setTag(position);
holder.btnRemoveFavourite.setTag(position);
Product state = productItems.get(position);
System.out.println("Product position -----> " + state);
if (state.isSelected()) {
System.out.println("Selected state ******************* " + state.isSelected());
System.out.println("selected");
holder.btnAddFavourite.setVisibility(View.VISIBLE);
holder.btnRemoveFavourite.setVisibility(View.GONE);
} else {
System.out.println("Selected state ******************* " + state.isSelected());
System.out.println(" not selected");
holder.btnAddFavourite.setVisibility(View.GONE);
holder.btnRemoveFavourite.setVisibility(View.VISIBLE);
}
convertView.setTag(holder);
return convertView;
}
final Product product = (Product) productItems.get(position);
//final Product product = (Product) getItem(position);
holder.thumbNail.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
holder.title.setText(product.getName());
Picasso.with(activity).load(product.getHref())
.placeholder(R.drawable.ic_default_banner)
.into(holder.thumbNail);
private class ProductHolder {
TextView title;
ImageView thumbNail;
ImageButton btnAddFavourite;
ImageButton btnRemoveFavourite;
}
}
答案 0 :(得分:0)
在按钮onclick功能中更改数据集后,必须使用youradapetr.notifydatasetchanged() ,在您的情况下,只需添加notifyDataSetChanged();如下所示两个函数
holder.btnAddFavourite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("Add favourities");
int pos = (Integer) v.getTag();
productItems.get(pos).setSelected(false);
holder.btnAddFavourite.setVisibility(View.GONE);
holder.btnRemoveFavourite.setVisibility(View.VISIBLE);
notifyDataSetChanged();
}
});
答案 1 :(得分:0)
我认为问题出在onClickListener中。使用作为参数而不是pos的位置来设置监听器内的状态。
holder.btnAddFavourite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("Add favourities");
productItems.get(position).setSelected(false);
holder.btnAddFavourite.setVisibility(View.GONE);
holder.btnRemoveFavourite.setVisibility(View.VISIBLE);
}
});
holder.btnRemoveFavourite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("Remove favorities");
productItems.get(position).setSelected(true);
holder.btnAddFavourite.setVisibility(View.VISIBLE);
holder.btnRemoveFavourite.setVisibility(View.GONE);
}
});
编辑1: -
你可以尝试下面的getView方法。我认为你在onclick监听器中设置了错误的状态。您在btnRemoveFavourite中设置setSelected(true)但它应该是setSelected(false)。同样适用于btnAddFavorite。
如果正确,请尝试以下代码并接受答案。
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ProductHolder holder = null;
if (convertView == null) {
holder = new ProductHolder();
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.product_list_row_item, parent, false);
holder.thumbNail = (ImageView) convertView.findViewById(R.id.product_image);
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.btnAddFavourite = (ImageButton) convertView.findViewById(R.id.AddFavourites);
holder.btnRemoveFavourite = (ImageButton) convertView.findViewById(R.id.RemoveFavourites);
convertView.setTag(holder);
} else {
holder = (ProductHolder) convertView.getTag();
}
final Product product = productItems.get(position);
holder.btnAddFavourite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
product.setSelected(true);
holder.btnAddFavourite.setVisibility(View.GONE);
holder.btnRemoveFavourite.setVisibility(View.VISIBLE);
}
});
holder.btnRemoveFavourite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
product.setSelected(false);
holder.btnAddFavourite.setVisibility(View.VISIBLE);
holder.btnRemoveFavourite.setVisibility(View.GONE);
}
});
if (product.isSelected()) {
System.out.println("Selected state ******************* " + state.isSelected());
System.out.println("selected");
holder.btnAddFavourite.setVisibility(View.GONE);
holder.btnRemoveFavourite.setVisibility(View.VISIBLE);
} else {
System.out.println("Selected state ******************* " + state.isSelected());
System.out.println(" not selected");
holder.btnAddFavourite.setVisibility(View.VISIBLE);
holder.btnRemoveFavourite.setVisibility(View.GONE);
}
return convertView;
}
答案 2 :(得分:0)
问题:
您的适配器中的getView()会不时被调用。因此,如果没有为按钮设置选定状态,它将恢复到原始状态。
解决方案:
使用getter和setter在模型类[Product]中保留一个布尔值:
boolean didSelect;
public boolean isDidSelect() {
return didSelect;
}
public void setDidSelect(boolean didSelect) {
this.didSelect = didSelect;
}
在getView()中,根据布尔值中的值设置按钮的选择。
答案 3 :(得分:0)
这里我给你一个简单的演示,你可以根据你的要求更新它,我希望你能得到你的解决方案。
公共类MainActivity扩展了Activity {
String s[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
"M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y",
"Z" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Product> products = new ArrayList<Product>();
ListView list = (ListView) findViewById(R.id.listview);
for (int i = 0; i < s.length; i++) {
Product item = new Product();
item.setCheck(false);
item.setName(s[i].toString());
products.add(item);
}
ProductListAdapter ac = new ProductListAdapter(MainActivity.this, R.layout.row,
products);
list.setAdapter(ac);
}// on create method end
class ProductListAdapter extends BaseAdapter {
private List<Product> productItems;
Context context;
int layoutId;
public ProductListAdapter(Context context, int layoudId, List<Product> productItems) {
// super(context,productItems);
this.layoutId = layoudId;
this.context = context;
this.productItems = productItems;
}
public class ProductHolder {
ImageView image;
TextView name;
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
final ProductHolder holder;
Product rowitems = productItems.get(position);
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(layoutId, parent, false);
holder = new ProductHolder();
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.image = (ImageView) convertView
.findViewById(R.id.invite_click);
holder.image.setBackgroundResource(R.drawable.green);
convertView.setTag(holder);
}
else {
holder = (ProductHolder) convertView.getTag();
}
holder.image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (productItems.get(position).isCheck) {
productItems.get(position).isCheck = false;
// holder.image.setImageBitmap(null);
holder.image.setBackgroundResource(R.drawable.green);
} else {
productItems.get(position).isCheck = true;
holder.image
.setBackgroundResource(R.drawable.green_tick);
}
}
});
if (productItems.get(position).isCheck) {
productItems.get(position).isCheck = true;
holder.image.setBackgroundResource(R.drawable.green_tick);
} else {
productItems.get(position).isCheck = false;
holder.image.setImageBitmap(null);
holder.image.setBackgroundResource(R.drawable.green);
}
holder.name.setText(rowitems.getName());
Log.e("name----->", productItems.get(position).toString());
return convertView;
}
@Override
public int getCount() {
return productItems.size();
}
@Override
public Object getItem(int position) {
return productItems.get(position);
}
@Override
public long getItemId(int position) {
return productItems.indexOf(getItem(position));
}
}
}