如何在快速适配器库中切换视图颜色

时间:2017-08-12 18:36:32

标签: android onclick android-recyclerview multi-select fastadapter

我想为我的回收站视图实现多选适配器。我正在使用Mike Penz的FastAdapter。

我不确定如何切换所选的项目视图颜色。

以下是代码段,将ClickEventHook<SimpleItem>附加到项目中的卡片视图,以便在点击卡片视图时调用onClick()

private void setupFastAdapter() {
    mFastAdapter = new FastAdapter<>();

    // Configure the FastAdapter.
    mFastAdapter.withSelectable(true);
    mFastAdapter.withMultiSelect(true);
    mFastAdapter.withSelectOnLongClick(false);

    mFastAdapter.withSelectionListener(new ISelectionListener<SimpleItem>() {
        @Override
        public void onSelectionChanged(SimpleItem item, boolean selected) {
            // Toggle the card and text colors.
            if (selected) {
                Logger.i("Selected [%s]", item.getText());
            } else {
                Logger.i("Unselected [%s]", item.getText());
            }
        }
    });

    // Click listeners for views inside the item, add an `EventHook` to the `FastAdapter` by
    // implementing either a `ClickEventHook`, `LongClickEventHook`, `TouchEventHook`, `CustomEventHook`
    mFastAdapter.withEventHook(new ClickEventHook<SimpleItem>() {
        private CardView cardView;
        private TextView textView;

        @Nullable
        @Override
        public View onBind(@NonNull RecyclerView.ViewHolder viewHolder) {
            //return the views on which you want to bind this event
            if (viewHolder instanceof SimpleItem.ViewHolder) {
                cardView = ((SimpleItem.ViewHolder) viewHolder).mCardView;
                textView = ((SimpleItem.ViewHolder) viewHolder).mTextView;
                return cardView;
            } else {
                return null;
            }
        }

        @Override
        public void onClick(View v, int position, FastAdapter<SimpleItem> fastAdapter, SimpleItem item) {
            //react on the click event
            Logger.i("Clicked [%s]", item.getText());
            if (cardView.isSelected()) {
                cardView.setCardBackgroundColor(getActivity().getResources().getColor(R.color.app_green_dark));
                textView.setTextColor(getActivity().getResources().getColor(R.color.app_white));
            } else {
                cardView.setCardBackgroundColor(getActivity().getResources().getColor(R.color.app_light_blue_50));
                textView.setTextColor(getActivity().getResources().getColor(R.color.primary_text));
            }
        }
    });
}

卡片视图的颜色和文本视图的文本颜色不会切换。而且我不确定它是如何正确实现的。

我发现,当点击项目中的卡片视图时,也不会调用onSelectionChanged()方法。

有人可以建议我在选择和取消选择时切换卡片视图和文字的颜色吗?

提前谢谢你,
摩尼

1 个答案:

答案 0 :(得分:0)

我建议使用StateListDrawableStateListDrawable来设置州特定背景。

示例应用程序中使用了类似的东西。

您可以看到此类ColorDrawable clrActive = new ColorDrawable(selected_color); states.addState(new int[]{android.R.attr.state_selected}, clrActive); states.addState(new int[]{}, ContextCompat.getDrawable(ctx, getSelectableBackground(ctx))); here的简单实现:

StateListDrawable states = new StateListDrawable();

//if possible we enable animating across states
if (animate) {
    int duration = ctx.getResources().getInteger(android.R.integer.config_shortAnimTime);
    states.setEnterFadeDuration(duration);
    states.setExitFadeDuration(duration);
}

这甚至支持动画:

m2 <- mlogit(f2, lfsm2, weights=PWT14)