Multi select CardView in ListView

做~自己de王妃 提交于 2019-12-12 20:34:17

问题


I want to multi select custom ListView row, I designed it using CardView and all events happens on this card, I want to implement Contextual Action bar 'CAB' when the user long press the card, actually I made it but can't change the row color state when selected like this, I used the code from this answer:

and make CardView checkable using this code:

    public class CheckableCard extends CardView implements Checkable {
    private boolean mChecked;

    private final static int[] CHECKED_STATE_SET = {
        android.R.attr.state_checked
    };

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

    @Override
    protected int[] onCreateDrawableState(int extraSpace) {
        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        if (isChecked()) {
            mergeDrawableStates(drawableState, CHECKED_STATE_SET);
        }
        return drawableState;
    }

    @Override
    public void toggle() {
        setChecked(!mChecked);
    }

    @Override
    public boolean isChecked() {
        return mChecked;
    }

    @Override
    public void setChecked(boolean checked) {
        if (mChecked != checked) {
            mChecked = checked;
            refreshDrawableState();
        }
    }
}

Then in OnLongClickListener in the CardView I make it checked:

cardView.setChecked(true);

but nothing works!

Also I don't know how to multi select it, because it normal ListView row we use setMultiChoiceModeListener but when the events happens on card not list I don't know how to implement this.

I am a beginner and need help please.


回答1:


OK, here's what I did:

SelectableCardView

public class SelectableCardView extends CardView
  {
  private int _unselectedBackgroundColor;
  private int _selectedBackgroundColor;
  private boolean _isSelectedble;

  public SelectableCardView(Context context)
    {
    this(context,null);
    }

  public SelectableCardView(Context context,AttributeSet attrs)
    {
    this(context,attrs,0);
    }

  public SelectableCardView(Context context,AttributeSet attrs,int defStyleAttr)
    {
    super(context,attrs,defStyleAttr);
    final Resources res=context.getResources();
    _selectedBackgroundColor=...
    _unselectedBackgroundColor=... // you could use cardview_light_background or cardview_dark_background for the default values here, depending on your theme
    }

  public void setSelectedBackgroundColor(@ColorInt int selectedBackgroundColor)
    {
    _selectedBackgroundColor=selectedBackgroundColor;
    }

  public void setUnselectedBackgroundColor(@ColorInt int unselectedBackgroundColor)
    {
    _unselectedBackgroundColor=unselectedBackgroundColor;
    }

  @Override
  public void setSelected(final boolean selected)
    {
    super.setSelected(selected);
    if(_isSelectedble)
      setCardBackgroundColor(isSelected()?_selectedBackgroundColor:_unselectedBackgroundColor);
    }

  public void setSelectedble(final boolean selectedble)
    {
    if(!selectedble)
      {
      super.setSelected(false);
      setCardBackgroundColor(_unselectedBackgroundColor);
      }
    _isSelectedble=selectedble;
    }
  }

usage:

<...SelectableCardView
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:clickable="true"
  android:foreground="@drawable/card_foreground"
  app:cardCornerRadius="@dimen/card_radius"
  app:cardElevation="4dp"
  app:contentPadding="10dp">
  ...

In order to make a card as selected, just call setSelected(...) on it.

No magic tricks here...

The rest of the code is similar to the link you've given here



来源:https://stackoverflow.com/questions/33886756/multi-select-cardview-in-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!