【问题标题】:Card Presenter expands in Android 4.4 when selected选中后,Card Presenter 会在 Android 4.4 中展开
【发布时间】:2016-06-15 04:27:49
【问题描述】:

这是cardpresenter.java 的代码,此处指定了card_widthcard_height,但它在Android Kitkat 中没有遵循。如何让CardView兼容Android 4.4.3?

package com.example.sahilbhatoa.iitv;

import android.graphics.drawable.Drawable;
import android.support.v17.leanback.widget.ImageCardView;
import android.support.v17.leanback.widget.Presenter;
import android.util.Log;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;

/*
 * A CardPresenter is used to generate Views and bind Objects to them on demand.
 * It contains an Image CardView
 */
public class CardPresenter extends Presenter {
    private static final String TAG = "CardPresenter";

    private static final int CARD_WIDTH = 313;
    private static final int CARD_HEIGHT = 176;
    private static int sSelectedBackgroundColor;
    private static int sDefaultBackgroundColor;
    private Drawable mDefaultCardImage;

    private static void updateCardBackgroundColor(ImageCardView view, boolean selected) {
        int color = selected ? sSelectedBackgroundColor : sDefaultBackgroundColor;
        // 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 ViewHolder onCreateViewHolder(ViewGroup parent) {
        Log.d(TAG, "onCreateViewHolder");

        sDefaultBackgroundColor = parent.getResources().getColor(R.color.default_background);
        sSelectedBackgroundColor = parent.getResources().getColor(R.color.selected_background);
        mDefaultCardImage = parent.getResources().getDrawable(R.drawable.movie);

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

    @Override
    public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {

        ImageCardView cardView = (ImageCardView) viewHolder.view;
        cardView.setMainImageDimensions(CARD_WIDTH, CARD_HEIGHT);
        if (item instanceof Movie)
        {
            Movie movie = (Movie) item;
            Log.d(TAG, "onBindViewHolder");
            if (movie.getCardImageUrl() != null) {
                cardView.setTitleText(movie.getTitle());
                cardView.setContentText(movie.getCategory());
                cardView.setMainImageDimensions(CARD_WIDTH, CARD_HEIGHT);
                Glide.with(viewHolder.view.getContext())
                        .load(movie.getCardImageUrl())
                        .centerCrop()
                        .error(mDefaultCardImage)
                        .into(cardView.getMainImageView());
            }
        }
        else
        {
            cardView.setTitleText(item.toString());
            cardView.setMainImageDimensions(CARD_WIDTH, CARD_HEIGHT);

            if (item.toString().equalsIgnoreCase("categories"))
                Glide.with(viewHolder.view.getContext())
                    .load(R.drawable.categories)
                    .centerCrop()
                    .error(mDefaultCardImage)
                    .into(cardView.getMainImageView());
            else if (item.toString().equalsIgnoreCase("Account Summary"))
                Glide.with(viewHolder.view.getContext())
                        .load(R.drawable.account_summary)
                        .centerCrop()
                        .error(mDefaultCardImage)
                        .into(cardView.getMainImageView());
            else if (item.toString().equalsIgnoreCase("Video On Demand"))
                Glide.with(viewHolder.view.getContext())
                        .load(R.drawable.videoondemand)
                        .centerCrop()
                        .error(mDefaultCardImage)
                        .into(cardView.getMainImageView());
            else if (item.toString().equalsIgnoreCase("Customer Support"))
                Glide.with(viewHolder.view.getContext())
                        .load(R.drawable.customer_support)
                        .centerCrop()
                        .error(mDefaultCardImage)
                        .into(cardView.getMainImageView());
            else if (item.toString().equalsIgnoreCase("Favorites"))
                Glide.with(viewHolder.view.getContext())
                        .load(R.drawable.favourites)
                        .centerCrop()
                        .error(mDefaultCardImage)
                        .into(cardView.getMainImageView());

        }


    }

    @Override
    public void onUnbindViewHolder(Presenter.ViewHolder viewHolder) {
        Log.d(TAG, "onUnbindViewHolder");
        ImageCardView cardView = (ImageCardView) viewHolder.view;
        // Remove references to images so that the garbage collector can free up memory
        cardView.setBadgeImage(null);
        cardView.setMainImage(null);
    }
}

In this the Card view works perfectly with same code just OS is android 5.0.1

Card View expands to full screen. Here the OS is Android 4.4.3

【问题讨论】:

    标签: android android-tv


    【解决方案1】:

    我认为对此有待处理的bug report。 API 21 之前的所有 API 都可能存在此问题。您可能希望按照此更新有关上述问题的信息。同时,尝试使用 Android 版本 API 21 及更高版本。希望这会有所帮助

    【讨论】:

      猜你喜欢
      • 2014-06-12
      • 2018-02-27
      • 2020-10-21
      • 2014-06-27
      • 2014-08-14
      • 1970-01-01
      • 2018-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多