0

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

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

这是代码片段,将 a 附加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()单击项目中的卡片视图时也不会调用方法。

谁能建议我一种在选择和取消选择时切换卡片视图和文本颜色的方法?

提前谢谢你,
玛尼

4

1 回答 1

0

我建议使用 aColorStateList或 aStateListDrawable来设置特定状态的背景。

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

StateListDrawable 你可以在这里看到一个简单的实现:

StateListDrawable states = new StateListDrawable();

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

这甚至支持动画:

//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);
}
于 2017-08-13T19:04:26.937 回答