使用recyclerview-multiselect时消除CardViews

时间:2016-09-21 13:26:57

标签: android android-recyclerview

ValueError documentation是一个出色的库,用于管理多重选择和ActionMode中的RecyclerView

但是,如果将CardView用作项目,则在正常状态下(未激活)将变为不可见。如果它被激活,它显示正常 - 具有强调颜色。

我们如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

这可能是因为您在RecyclerViewAdapter中使用了SwappingHolder。此课程未正确设置CardView的背景色。

而是扩展MultiSelectorBindingHolder并覆盖如下的抽象方法:

class ViewHolder extends MultiSelectorBindingHolder implements View.OnClickListener,
        View.OnLongClickListener {
    final CardView cardView;

    private ColorStateList defaultCardBackground;
    private ColorStateList selectableCardBackground;
    private StateListAnimator defaultStateListAnimator;
    private StateListAnimator selectableStateListAnimator;
    private boolean selectable;

    ViewHolder(View view) {
        super(view, multiSelector);
        if (view instanceof CardView) {
            Log.d(TAG, "ViewHolder: got the cardView");
            cardView = (CardView) view;
        } else {
            throw new IllegalStateException("Expected a CardView!");
        }
        this.selectable = false;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            defaultStateListAnimator = itemView.getStateListAnimator();
            selectableStateListAnimator = Utils.getRaiseStateListAnimator(context);
        } else {
            defaultStateListAnimator = null;
            selectableStateListAnimator = null;
        }
        defaultCardBackground = cardView.getCardBackgroundColor();
        selectableCardBackground = Utils.getSelectableCardBackground(context);
    }

    @Override
    public void setSelectable(boolean selectable) {
        boolean changed = (selectable != this.selectable);
        if (changed) {
            Log.d(TAG, "setSelectable: changed");
            this.selectable = selectable;
            refreshChrome();
        }
    }

    private void refreshChrome() {
        Log.d(TAG, "refreshChrome : selectable = " + selectable + ", title:" + download.getTitle());

        ColorStateList cardBackgroundColor = this.selectable ?
                this.selectableCardBackground :this.defaultCardBackground;
        this.cardView.setCardBackgroundColor(cardBackgroundColor);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            StateListAnimator animator = this.isSelectable() ?
                this.selectableStateListAnimator : this.defaultStateListAnimator;
            this.cardView.setStateListAnimator(animator);
            if(animator != null) {
                animator.jumpToCurrentState();
            }
        }
    }

    @Override
    public boolean isSelectable() {
        return selectable;
    }

    @Override
    public void setActivated(boolean b) {
        cardView.setActivated(b);
    }

    @Override
    public boolean isActivated() {
        return cardView.isActivated();
    }
}