由于 API 级别 18 (JELLY_BEAN_MR2),您可以使用 ViewOverlay
https://developer.android.com/reference/android/view/ViewOverlay
https://developer.android.com/reference/android/view/View#getOverlay()
- 得到一个可绘制的
drawable = context.getDrawable(R.drawable.sample)
- 设置它的界限
drawable.setBounds(0, 0, 50, 50)
- 添加叠加层
view.getOverlay().add(drawable)
public class ImageCardViewPresenter extends Presenter {
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
ImageCardView imageCardView = new ImageCardView(parent.getContext());
imageCardView.setMainImageDimensions(100, 100);
return new ImageCardViewHolder(imageCardView);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, Object item) {
Model model = (Model) item;
ImageCardView imageCardView = (ImageCardView) viewHolder.view;
Context context = imageCardView.getContext();
imageCardView.setTitleText(model.getTitle());
imageCardView.setMainImage(context.getDrawable(model.getImageId());
Drawable badge = context.getDrawable(model.getBadgeId());
badge.setBounds(0, 0, 20, 20);
imageCardView.getOverlay().clear();
imageCardView.getOverlay().add(badge);
}
@Override
public void onUnbindViewHolder(ViewHolder viewHolder) {
ImageCardView imageCardView = (ImageCardView) viewHolder.view;
imageCardView.setMainImage(null);
imageCardView.getOverlay().clear();
}
public static class ImageCardViewHolder extends ViewHolder {
public ImageCardViewHolder(View view) {
super(view);
}
}
}
public class Model {
private String title;
@DrawableRes
private int imageId;
@DrawableRes
private int badgeId;
// getters and setters
}