簡體   English   中英

在ListView中多選CardView

[英]Multi select CardView in ListView

我想多選擇自定義ListView行,我使用CardView設計了它,並且所有事件都發生在此卡上,我想在用戶長按卡時實現Contextual Action bar 'CAB' ,實際上我做到了但不能更改像這樣選擇行顏色狀態時,我使用了以下答案中的代碼:

並使用以下代碼檢查CardView

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

然后在OnLongClickListenerCardView進行檢查:

cardView.setChecked(true);

但沒有任何效果!

我也不知道如何多重選擇它,因為它是正常的ListView行,我們使用setMultiChoiceModeListener但是當事件發生在卡上而不是列表上時,我不知道該如何實現。

我是初學者,需要幫助。

好的,這是我所做的:

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

用法:

<...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">
  ...

為了使卡被選中,只需在其上調用setSelected(...)。

這里沒有魔術!

其余代碼與您在此處提供的鏈接相似

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM