android - 回收站 View : how to add OnClick() and keep onLongClick() working?

标签 android checkbox android-recyclerview

我在 CheckBox 上为包含 CardView 列表的 RecyclerView 设置了一个 onClickListener。监听器在我的 ItemHolder 中设置,它扩展了 ViewHolder。初始单击 CardView 会检查 CheckBox 并将 CardView 的背景颜色从默认的白色切换为红色。这工作正常。

我还在 CardView 本身上设置了一个 OnClickListenerOnClickListeneronCreateViewHolder() 中设置。单击 CardView 会为 CardView 启动一个新的详细信息 Activity 。这工作正常。

最后,我尝试在 CardView 本身上设置一个 onLongClickListenerOnLongClickListener 在 onCreateViewHolder() 中设置。长按 CardView 意味着将背景颜色切换为红色并启动 AlertDialog,以便用户可以确认 CardView 将从列表中删除。这可以正常工作,但是当将此代码添加到适配器时,CardView 的 CheckBox 的 OnClickListerner 不再有效。好像 OnLongClickListner 与 CheckBox 监听器冲突。请注意,我在 itemHolder 的 onLongClick() 代码中“返回真值”。我在这里缺少什么?

适配器.java

public MyRecylerAdapter(Context context, ArrayList<ListItem> listItems, ArrayList<ListItem> selectedList) {
    this.mContext = context;
    this.mListItems = listItems;
    this.selectedItemsList = selectedList;
}

private int selectedPos = -1;
...

private class ItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    private CardView cardView;
    private CheckBox chkSelected;

    private ItemHolder(final View itemView) {
        super(itemView);

        cardView = (CardView) itemView.findViewById(R.id.singlecard_view1);
        chkSelected = (CheckBox) itemView.findViewById(R.id.chkSelected);
        chkSelected.setOnClickListener(this);
    }

    public void onClick(View v) {

        int adapterPos = getAdapterPosition();

        if (adapterPos == android.support.v7.widget.RecyclerView.NO_POSITION) return;
        if (recyclerItemClickListener !=null) {
            recyclerItemClickListener.onCheckBoxClick(v, adapterPos);
        }
        Integer iPos = adapterPos;

        if (((CheckBox)v).isChecked()) {
            checkedListItems.add(iPos);
        }
        else {
            checkedListItems.remove(iPos);
        }
    }

    void bind(int position) {

        if (checkedListItems.contains(position)) {
            chkSelected.setChecked(true);
        }
        else {
            chkSelected.setChecked(false);
        }
    }
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_contact_item, parent, false);
    final ItemHolder itemHolder = new ItemHolder(view);

        itemHolder.itemView.setOnClickListener(new View.OnClickListener() {
            // Handles the row being clicked.
            @Override
            public void onClick(View view) {

                ListItem adapterItem = MyRecylerAdapter.this.getItem(itemHolder.getAdapterPosition()); 

                if (recyclerItemClickListener != null) {
                    recyclerItemClickListener.onItemClick(itemHolder.itemView, adapterItem);
                }
            }
        });

        itemHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {

                ListItem adapterItem2 = MyRecylerAdapter.this.getItem(itemHolder.getAdapterPosition()); 

                    if (recyclerItemClickListener != null) {
                        recyclerItemClickListener.onItemLongClick(itemHolder.itemView, adapterItem2);
                    }

                int adapterPos2 = itemHolder.getAdapterPosition(); 
                if (adapterPos2 != android.support.v7.widget.RecyclerView.NO_POSITION) {
                    int lastSelectedPosition = selectedPos;
                    selectedPos = adapterPos2;
                    notifyItemChanged(lastSelectedPosition);
                    notifyItemChanged(selectedPos);
                }                    
                return true;
            }
        });
    return itemHolder;
}

public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {

    final ListItem listItem = mListItems.get(position);
    final ItemHolder itemHolder = (ItemHolder) holder;

    itemHolder.bind(position);

    if (checkedListItems.contains(position)) {
        itemHolder.cardView.setActivated(true);
    }
    else {
        itemHolder.cardView.setActivated(false);
    }        

    // **The addition of the below code causes the "itemHolder.cardView.
    // setActivated(true);" in onBindViewHolder method to no longer fire, as 
    // a click on the CheckBox no longer changes the CardView background 
    // color.**
    if (itemHolder.getAdapterPosition() == selectedPos) {
        itemHolder.cardView.setActivated(true);
    } else {
        itemHolder.cardView.setActivated(false);
    }

list_contact_item.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/singlecard_view1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:foreground="?android:attr/selectableItemBackground"
     >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/statelist_cardview_background"  >

    <CheckBox
        android:id="@+id/chkSelected"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="4dp"
        android:layout_marginStart="4dp"
        android:layout_marginTop="4dp"
        android:gravity="center"  />

    <TextView
        android:id="@+id/cardType1"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_toRightOf="@+id/chkSelected"
        android:layout_toEndOf="@+id/chkSelected"
        android:layout_alignParentTop="true"
        android:paddingStart="3dp"
        android:paddingLeft="3dp"
        android:paddingEnd="6dp"
        android:paddingRight="6dp"
        android:layout_marginTop="4dp"
        android:gravity="center"
        android:textColor="#ffffff"
        android:textStyle="bold|italic"
        style="@style/Base.TextAppearance.AppCompat.Subhead"  />

    <TextView
        android:id="@+id/cardBlankText1"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/cardType1"
        android:layout_toEndOf="@+id/cardType1"
        android:layout_toLeftOf="@+id/cardBlankTextNumstotal"
        android:layout_toStartOf="@+id/cardBlankTextNumstotal"
        android:layout_marginTop="4dp"
        android:gravity="center_vertical|end"
        android:text="#"
        android:textColor="@color/colorFlLabelFinal"
        android:textStyle="bold"
        android:maxLines="1"
        style="@style/Base.TextAppearance.AppCompat.Subhead"  />

    <TextView
        android:id="@+id/cardBlankTextNumstotal"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:gravity="center"
        android:text="actual card #"
        android:layout_marginTop="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginEnd="4dp"
        android:freezesText="true"
        android:textColor="@android:color/black"
        android:maxLines="1"
        style="@style/Base.TextAppearance.AppCompat.Subhead"  />        

    <TextView
        android:id="@+id/cardBlankText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/chkSelected"
        android:layout_marginTop="4dp"
        android:layout_marginLeft="6dp"
        android:layout_marginStart="6dp"
        android:text="todo"
        android:textColor="@android:color/black"
        android:textStyle="bold"
        android:background="@drawable/todo_underline"
        android:maxLines="1"
        style="@style/Base.TextAppearance.AppCompat.Headline"  />

    ...
    </RelativeLayout>

</android.support.v7.widget.CardView>

statelist_cardview_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_activated="true"

        android:drawable="@color/item_selected" />

    <item android:state_activated="false"

        android:drawable="@color/list_contact_item_default"  />

</selector>

颜色.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <color name="list_contact_item_default">#FFFFFF</color>
    <color name="item_selected">#FF0000</color>

</resources>

最佳答案

How come you are not using OnCheckedChangeListener for checkbox?

CheckBox chkBox = ( CheckBox ) findViewById( R.id.checkbox );
chkBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        if ( isChecked )
        {
            // perform logic
        }

    }
});

关于android - 回收站 View : how to add OnClick() and keep onLongClick() working?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47783631/

相关文章:

java - 如何关闭popupWindow?

Android onClick 复选框未在 ListView 中选中

javascript - Jquery 使用图像作为复选框

java - 无法从内存中检索 mp3 文件

java - 注销广播接收器错误

Android CookieManager setCookie 创建多个cookie

android - 如何实现多个Android版本使用不同的库?

java - 将复选框值保存到 sqlite 数据库

java - 如何从另一个 fragment 中对适配器进行排序

android - 未触发嵌套的 RecyclerView onClick