0
好日子,我正在写一个android程序的在线优惠券服务。我目前正在创建一个允许用户为该项目添加书签的页面。之后,他们可以在他们的书签文件夹中看到它。我遇到的问题是在单击书签并选择特定项目后。但是当我向上滚动选定的书签变为取消选择状态。我怎样才能防止这种情况发生。下面是我的代码如何保存按钮状态recyclerview
Coupon.java
public class Coupon {
private String company_name;
private String offer_desc;
public Coupon() {
}
public Coupon(String company_name, String offer_desc) {
this.company_name = company_name;
this.offer_desc = offer_desc;
}
public String getCompany_name() {
return company_name;
}
public void setCompany_name(String company_name) {
this.company_name = company_name;
}
public String getOffer_desc() {
return offer_desc;
}
public void setOffer_desc(String offer_desc) {
this.offer_desc = offer_desc;
}
}
CouponViewHolder.java
public class CouponViewHolder extends RecyclerView.ViewHolder{
protected TextView company_name;
protected TextView offer_desc;
protected LikeButton star_button;
protected LikeButton heart_button;
public CouponViewHolder(final View item){
super(item);
company_name = (TextView) item.findViewById(R.id.company_name);
offer_desc = (TextView) item.findViewById(R.id.offer_desc);
star_button = (LikeButton) item.findViewById(R.id.star_button);
heart_button = (LikeButton) item.findViewById(R.id.heart_button);
}
}
CouponAdapter.java
public class CouponAdapter extends RecyclerView.Adapter<CouponViewHolder>{
private List<Coupon> couponList;
public CouponAdapter(List<Coupon> couponList) {
this.couponList = couponList;
}
@Override
public CouponViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.fragment_one_card, parent, false);
CouponViewHolder vh = new CouponViewHolder(v);
return vh;
}
@Override
public long getItemId(int position) {
return super.getItemId(position);
}
@Override
public void onBindViewHolder(CouponViewHolder holder, int position) {
Coupon coupon = couponList.get(position);
holder.company_name.setText(coupon.getCompany_name());
holder.offer_desc.setText(coupon.getOffer_desc());
holder.heart_button.setLiked(false);
holder.star_button.setLiked(false);
holder.heart_button.setOnLikeListener(new OnLikeListener() {
@Override
public void liked(LikeButton likeButton) {
notifyDataSetChanged();
}
@Override
public void unLiked(LikeButton likeButton) {
notifyDataSetChanged();
}
});
}
@Override
public int getItemCount() {
return couponList.size();
}
}
谢谢
您可以将按钮的状态存储在您的本地内存,sqllite或sharedprefrences或者你可以把它在乌拉圭回合服务器DB – Arshad
您的优惠券使用布尔isSelected状态,这将持有的每个用户,每个优惠券的按钮的状态并在点击按钮的同时更改它。 –
你有任何示例代码? – user10908