我一直在使用android电视应用程序工作。我有在BrowserFragment类中填充的电影列表。我有一个演示者类,该类使用ImageCardView来显示电影缩略图。我正在尝试自定义imagecardview我希望它看起来像这里的图片。选择特定的ImageCardView时,缩略图的大小会增加,但基本对齐方式应与其他未选中的对齐,如pic中所示。

编辑图片链接https://www.dropbox.com/s/habpjnrlwh1se8v/sample.png?dl=0

EDIT Presenter类

public class CardPresenter extends Presenter {
private int mSelectedBackgroundColor = -1;
private int mDefaultBackgroundColor = -1;
private Drawable mDefaultCardImage;

class ViewHolder extends Presenter.ViewHolder {
    HashMap<Integer,Object> ObjectsList=new HashMap<>();
    public ViewHolder(View view) {
        super(view);
    }
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
    mDefaultBackgroundColor = R.color.default_background;
    mSelectedBackgroundColor = R.color.selected_background;
    mDefaultCardImage = parent.getResources().getDrawable(R.drawable.lb_card_shadow_focused, null);

    ImageCardView cardView = new ImageCardView(parent.getContext()) {
        @Override
        public void setSelected(boolean selected) {
            updateCardBackgroundColor(this, selected);
            super.setSelected(selected);
        }
    };

    cardView.setFocusable(true);
    cardView.setFocusableInTouchMode(true);
    updateCardBackgroundColor(cardView, false);
    return new ViewHolder(cardView);
}

private void updateCardBackgroundColor(ImageCardView view, boolean selected) {
    int color = selected ? mSelectedBackgroundColor : mDefaultBackgroundColor;

    // Both background colors should be set because the view's
    // background is temporarily visible during animations.

    view.setBackgroundColor(color);
    view.findViewById(R.id.info_field).setBackgroundColor(color);
}

@Override
public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {
    Movie movie = (Movie) item;

    ImageCardView cardView = (ImageCardView) viewHolder.view;
    cardView.setTitleText(movie.getName());
    cardView.setContentText(movie.getCategory());

    if (movie.getImgResUrl() != 0) {
        // Set card size from dimension resources.
        Resources res = cardView.getResources();
//            int width = res.getDimensionPixelSize(R.dimen.card_width);
//            int height = res.getDimensionPixelSize(R.dimen.card_height);
        cardView.setMainImageDimensions(300, 300);

        Glide.with(cardView.getContext())
                .load(movie.getImgResUrl())
                .error(mDefaultCardImage)
                .into(cardView.getMainImageView());
    }
}

@Override
public void onUnbindViewHolder(Presenter.ViewHolder viewHolder) {
    ImageCardView cardView = (ImageCardView) viewHolder.view;

    // Remove references to images so that the garbage collector can free up memory.
    cardView.setBadgeImage(null);
    cardView.setMainImage(null);
}
}


任何帮助将不胜感激。谢谢

最佳答案

尝试使用此方法。

在您的View Holder类中,对此进行修改。

class ViewHolder extends Presenter.ViewHolder {
   private ImageCardView mCardView;
   private Drawable mDefaultCardImage;
   private PicassoImageCardViewTarget mImageCardViewTarget;
   HashMap<Integer,Object> ObjectsList=new HashMap<>();

   public ViewHolder(View view) {
        super(view);
        mCardView = (ImageCardView) view;
        mImageCardViewTarget = new PicassoImageCardViewTarget(mCardView);
    }

    protected void updateCardViewImage(URI uri) {
        Picasso.with(mContext)
                .load(uri.toString())
                .resize(Utils.convertDpToPixel(mContext, CARD_WIDTH),
                        Utils.convertDpToPixel(mContext, CARD_HEIGHT))
                .error(mDefaultCardImage)
                .into(mImageCardViewTarget);
    }
}


在演讲者类别中应全局定义卡片的宽度和高度。

private static int CARD_WIDTH = 323;
private static int CARD_HEIGHT = 146;


然后像这样在onBindViewHolder中调用它。

if (movie.getImgResUrl() != 0) {
    Resources res = cardView.getResources();
    ((ViewHolder) viewHolder).mCardView.setMainImageDimensions(CARD_WIDTH, CARD_HEIGHT);
    ((ViewHolder) viewHolder).updateCardViewImage(movie.getCardImageURI());
}


不要忘记在Build.Gradle文件中添加它。

compile 'com.squareup.picasso:picasso:2.3.2'


希望这会帮助你。

07-26 08:41