I have an onClickListener installed in a CheckBox for a RecyclerView that contains a CardView list. The listener is configured in my ItemHolder , which extends ViewHolder . An initial click on CardView checks the CheckBox and switches the CardView background color from white to red by default. This is working correctly.
I also have onClickListener installed in CardView itself. onClickListener configured in onCreateViewHolder() . Clicking on the CardView button launches a new verbose action for CardView . This is working correctly.
Finally, I tried onLongClickListener to CardView. onLongClickListener configured in onCreateViewHolder (). A long press on CardView is intended to switch the background color to red and start AlertDialog so that the user can confirm that CardView will be removed from the list. This works correctly, but when this code is added to the adapter, then OnClickListerner for CardView CheckBox no longer works. It looks like OnLongClickListner is in conflict with the CheckBox listener. Note. I am doing "return true" in the code of itemHolder onLongClick() . What am I missing here?
Adapter.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() {
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>
colors.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="list_contact_item_default">#FFFFFF</color> <color name="item_selected">#FF0000</color> </resources>
android checkbox android-recyclerview
AJW Dec 13 '17 at 0:13 2017-12-13 00:13
source share