Double-click OnClickListener of the Recycler View Adapter

I am trying to add a simple “View” view to a recycler view element, but for some reason I need to double-click the element and not once to complete the action. With one click, it seems that recycler View does not detect a click. On the next, however, it detects a click and performs a suitable action.

XML:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/cardView" android:layout_width="match_parent" android:layout_height="wrap_content" > <RelativeLayout android:id="@+id/rlContainer" android:layout_width="match_parent" android:layout_height="wrap_content" android:clickable="true" android:focusable="true" android:focusableInTouchMode="true" android:background="@drawable/selector_inventory_recycler_item" android:padding="16dp"> <ImageView android:id="@+id/item_photo" android:layout_width="100dp" android:layout_height="100dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginRight="16dp" /> <TextView android:id="@+id/txtItemName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/item_photo" android:textSize="16sp" /> <TextView android:id="@+id/txtItemQuantity" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/txtItemName" android:layout_toRightOf="@+id/item_photo" /> <TextView android:id="@+id/txtItemPrice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/txtItemQuantity" android:layout_toRightOf="@+id/item_photo" /> </RelativeLayout> </android.support.v7.widget.CardView> 

CODE:

  public class InventoryItemRecyclerAdapter extends RecyclerView.Adapter<InventoryItemRecyclerAdapter.InventoryItemViewHolder> { onItemClickListener mOnItemClickListener = null; /** * */ public ArrayList<Product> mInventoryItemList; Context mContext; static String TAG = "InventoryItemRecyclerAdapter"; Random random = new Random(); // ------------------------------------------------------------------------- // Constructor /** * * @param pInventoryItemList */ public InventoryItemRecyclerAdapter(ArrayList<Product> pInventoryItemList) { mInventoryItemList = pInventoryItemList; } // --------------------------------------------------------------------- @Override public InventoryItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { mContext = parent.getContext(); // Inflate the Layout for an item View v = LayoutInflater.from(parent.getContext()) .inflate(R.layout.item_inventory_recycler_adapter, parent, false); // Instantiate ViewHolder InventoryItemViewHolder inventoryItemViewHolder = new InventoryItemViewHolder(v); return inventoryItemViewHolder; } @Override public void onBindViewHolder(InventoryItemViewHolder holder, int position) { ... } // --------------------------------------------------------------------------------------------- /** * Returns the total number of items in the data set hold by the adapter. * * @return The total number of items in this adapter. */ @Override public int getItemCount() { return mInventoryItemList.size(); } // --------------------------------------------------------------------------------------------- // View Holder /** * RecyclerView */ public class InventoryItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { // ----------------------------------------------------------------------------------------- // Vars public CardView cardView; public RelativeLayout uiContainer; public TextView productName; public TextView productPrice; public TextView productQuantity; public ImageView productImage; public Product mProduct; // ----------------------------------------------------------------------------------------- // Constructor public InventoryItemViewHolder(View itemView) { super(itemView); cardView = (CardView) itemView.findViewById(R.id.cardView); productName = (TextView) itemView.findViewById(R.id.txtItemName); productImage = (ImageView) itemView.findViewById(R.id.item_photo); productPrice = (TextView) itemView.findViewById(R.id.txtItemPrice); productQuantity = (TextView) itemView.findViewById(R.id.txtItemQuantity); uiContainer = (RelativeLayout) itemView.findViewById(R.id.rlContainer); uiContainer.setOnClickListener(this); } // ----------------------------------------------------------------------------------------- /** * Called when a view has been clicked. * * @param v The view that was clicked. */ @Override public void onClick(View v) { Log.e("InventoryItemRecyclerAdapter", "onItemClick"); // Throw a null pointer exception if this is null if (mOnItemClickListener == null) { throw new NullPointerException("mOnItemClickListener is null in InventoryItemRecyclerAdapter"); } // Delegate to its caller. Let it handle the work mOnItemClickListener.onRecyclerViewItemClick(this); } // ------------------------------------------------------------- } // ----------------------------------------------------------------- /** * Interface for RecyclerView */ public interface onItemClickListener { /** * * @param pItemViewHolder */ public void onRecyclerViewItemClick(InventoryItemRecyclerAdapter.InventoryItemViewHolder pItemViewHolder); } } 

I could not find the problem that causes this problem. May I get help on this. Thanks.

+6
source share
2 answers

So, I have found a problem. The next two tags were the culprits here

 android:focusable="true" android:focusableInTouchMode="true" 

When we set focusable and focusableInTouchMode = true, this means that you turn on the view to focus first on the touch and then click.

+13
source

I had a similar problem, but the solution of setting focusable to false did not work in my case.

Instead, my problem was that I installed the onItemTouch listener in the exercise in which my recycler was stored.

 recyclerView.addOnItemTouchListener(...); 

Having removed this from my activity, my other touch listeners began to respond to individual clicks and not only double.

+3
source

Source: https://habr.com/ru/post/989561/


All Articles