课前准备
- 给 HomePagerFragment 和 RecyclerView 的 item 都添加上背景色
- 给 RecyclerView 设置间距
mContentList.addItemDecoration(new RecyclerView.ItemDecoration() {
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view,
@NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
outRect.top = 8;
outRect.bottom = 8;
}
});
课堂笔记
public class InnerHolder extends RecyclerView.ViewHolder {
@BindView(R.id.goods_cover)
public ImageView cover;
@BindView(R.id.goods_title)
public TextView title;
@BindView(R.id.tv_off_prise)
public TextView offPrice;
@BindView(R.id.after_off_prise)
public TextView afterOffPrice;
@BindView(R.id.original_prise)
public TextView originalPrice;
@BindView(R.id.goods_sell_count)
public TextView sellCount;
public InnerHolder(@NonNull View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
public void setData(HomePagerContent.DataBean dataBean) {
Context context = itemView.getContext();
//商品图片,创建方法UrlUtils.getCoverPath,给url加"https"前缀
Glide.with(context).load(UrlUtils.getCoverPath(dataBean.getPict_url())).into(cover);
//商品标题
title.setText(dataBean.getTitle());
//优惠券金额
int couponAmount = dataBean.getCoupon_amount();
offPrice.setText(String.format(context.getString(R.string.text_goods_off_price), couponAmount));
//商品原价
String zkFinalPrice = dataBean.getZk_final_price();
originalPrice.setText(String.format(context.getString(R.string.text_goods_original_price), zkFinalPrice));
//设置中划线
originalPrice.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
//商品券后价(原价-优惠券金额)
float zkAfterOffPrice = Float.valueOf(zkFinalPrice) - couponAmount;
//券后价金额保留两位小数
afterOffPrice.setText(String.format("%.2f", zkAfterOffPrice));
//商品销量
sellCount.setText(String.format(context.getString(R.string.text_goods_sell_count),dataBean.getVolume()));
}
<string name="text_goods_off_price">省%1$d元</string>
<string name="text_goods_original_price">¥%1$s</string>
<string name="text_goods_sell_count">%1$d人已经购买</string>