如何保存按钮状态recyclerview

时间:2016-01-24 15:57:56

标签: android android-recyclerview

好日子,我正在为在线优惠券服务编写一个Android程序。我目前正在创建一个页面,允许用户为项目添加书签。然后,他们可以在他们的书签文件夹中看到它。我遇到的问题是在单击书签并为特定项目选择之后。但当我向上滚动时,所选书签变为取消选择状态。我怎样才能防止这种情况发生。以下是我的代码

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();
    }

}

谢谢

1 个答案:

答案 0 :(得分:0)

您必须将所选内容保存在数据库或共享首选项或您使用的任何保存方法上。您可以创建一个界面,以便对您的课程或活动进行此操作。