ListView中多选择CardView [英] Multi select CardView in ListView

查看:679
本文介绍了ListView中多选择CardView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要多选择自定义的ListView 行,我设计它使用 CardView 键,所有的事件发生在此卡上,我想实施上下文操作栏CAB当用户长preSS卡,其实我做到了,但不能改变该行的颜色选择状态时像<一个href=\"http://theopentutorials.com/totwp331/wp-content/uploads/android-contextual-action-bar-with-listview-long-$p$pss_3694/android_cab_listview_delete_selection_output.png?c3a9c1\"相对=nofollow>这,我用code从这个回答

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:

和使 CardView 可勾选使用这个code:

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

然后在 OnLongClickListener CardView 我让检查:

cardView.setChecked(true);

但没有任何工程!

but nothing works!

此外,我不知道如何多选它,因为它,我们使用普通的的ListView setMultiChoiceModeListener ,但当事件发生在卡就不一一列举,我不知道如何实现这一点。

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.

推荐答案

确定,这里就是我所做的:

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

用法:

<...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(...)就可以了。

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

没有魔术在这里...

No magic tricks here...

在code的其余部分类似于你给 这里

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

这篇关于ListView中多选择CardView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆