11

选择卡片时,我正在尝试更改Android Leanback 库中ImageCardView 上infoArea的背景颜色。目前我尝试的是改变OnItemViewSelectedListener的背景。这会更改背景,但不会清除先前选择的项目。

private final class ItemViewSelectedListener implements OnItemViewSelectedListener {
        @Override
        public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item,
                                   RowPresenter.ViewHolder rowViewHolder, Row row) {
            if (item instanceof Video) {
                mBackgroundURI = ((Video) item).getBackgroundImageURI();
                startBackgroundTimer();
                ((ImageCardView) itemViewHolder.view)
                        .setInfoAreaBackgroundColor(getResources().getColor(R.color.dark_blue_grey));
            }
        }
    }

我想实现这样的目标:

想要的效果

有任何想法吗?谢谢。

4

3 回答 3

6

我找到了一个更简单的解决方案,我只跟踪当前选择的视图,然后根据它更改背景区域。

private final class ItemViewSelectedListener implements OnItemViewSelectedListener {

        private ImageCardView currentlySelectedView = null;

        @Override
        public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item,
                                   RowPresenter.ViewHolder rowViewHolder, Row row) {
            if (item instanceof Video) {
                mBackgroundURI = ((Video) item).getBackgroundImageURI();
                startBackgroundTimer();

                if (currentlySelectedView != null) {
                    currentlySelectedView.setInfoAreaBackgroundColor(
                            getResources().getColor(R.color.lb_basic_card_info_bg_color));
                }

                currentlySelectedView = (ImageCardView) itemViewHolder.view;
                currentlySelectedView
                        .setInfoAreaBackgroundColor(getResources().getColor(R.color.dark_blue_grey));
            }
        }
    }
于 2015-01-23T14:23:19.977 回答
5

我通过扩展 ImageCardView 来实现这一点,以保存自定义选择的颜色。

public static class CustomImageCardView extends ImageCardView {

    private int mColor;

    public CustomImageCardView(Context context) {
        super(context);
    }

    public CustomImageCardView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomImageCardView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public int getCustomSelectedSwatch() {
        return mColor;
    }

    public void setCustomColor(int color) {
        mColor = color;
    }
}

我将默认背景颜色和默认选定颜色保持为我的演示者中的成员变量。

private final int mDefaultInfoBackgroundColor;
private final int mDefaultSelectedInfoBackgroundColor;

并覆盖卡片图像视图的 setSelected 方法:

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
    mContext = parent.getContext();

    final CustomImageCardView cardView = new CustomImageCardView(mContext) {
        @Override
        public void setSelected(boolean selected) {
            if (getCustomColor() != 0 && selected) {
                setInfoAreaBackgroundColor(getCustomColor());
            } else setInfoAreaBackgroundColor(selected ? mDefaultSelectedInfoBackgroundColor : mDefaultInfoBackgroundColor);
            super.setSelected(selected);
        }
    };

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

如果您有任何问题,请告诉我!

于 2015-01-22T22:07:04.987 回答
2

如果您希望动态更改焦点卡片视图的视觉样式,您可以在 ImageCardView 上设置 OnFocusChangeListener。完整示例可以在Google 示例leaback-showcase项目中找到。

这是一个简短的示例,在您的 ImageCardViewPresenter 类中放置如下内容:

@Override
public Presenter.ViewHolder onCreateViewHolder(ViewGroup parent) {
    Context context = parent.getContext();

    ImageCardView cardView = new ImageCardView(context);
    cardView.setFocusable(true);
    cardView.setFocusableInTouchMode(true);

    cardView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                // Set bg color for the whole card
                cardView.setBackgroundColor(ContextCompat.getColor(context, R.color.FOCUSED_COLOR));
                // Set bg color for the info area only
                cardView.setInfoAreaBackgroundColor(ContextCompat.getColor(context, R.color.FOCUSED_COLOR));
                // Set title text color
                ((TextView) cardView.findViewById(R.id.title_text)).setTextColor(ContextCompat.getColor(context, R.color.FOCUSED_COLOR));
                // Set description text color
                ((TextView) cardView.findViewById(R.id.content_text)).setTextColor(ContextCompat.getColor(context, R.color.FOCUSED_COLOR));
            } 
            else {
                cardView.setBackgroundColor(ContextCompat.getColor(context, R.color.NORMAL_COLOR));
                cardView.setInfoAreaBackgroundColor(ContextCompat.getColor(context, R.color.NORMAL_COLOR));
                ((TextView) cardView.findViewById(R.id.title_text)).setTextColor(ContextCompat.getColor(context, R.color.NORMAL_COLOR));
                ((TextView) cardView.findViewById(R.id.content_text)).setTextColor(ContextCompat.getColor(context, R.color.NORMAL_COLOR));
            }
        }
    });

    return new ViewHolder(cardView);
}

要获得所有卡片子视图的正确布局 ID,请查看 XML 源代码,例如:lb_image_card_view.xml

于 2017-09-18T09:46:59.243 回答