选择时,Card Presenter会在android 4.4中展开

时间:2016-06-15 04:27:49

标签: android android-tv

这是cardpresenter.java的代码,这里指定了card_width和height但在android kitkat中没有跟随

`

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

1 个答案:

答案 0 :(得分:0)

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

相关问题