6

Sto tentando di aggiungere una semplice vista di clic a un elemento di una visualizzazione di riciclaggio, ma per qualche motivo devo fare clic su un elemento due volte anziché una volta per eseguire un'azione. Con un solo clic sembra che il riciclatore View non rilevi un clic. Sul successivo tuttavia rileva il clic ed esegue un'azione adeguata.Fare clic su OnClickListener doppio di un adattatore di visualizzazione Recycler

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> 

CODICE:

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

     } 

    } 

non riuscivo a trovare il problema che causa questo è il problema. Posso avere qualche aiuto su questo per favore. Grazie.

+0

Ciao, prova questo: itemView.setOnClickListener (this); –

+0

Grazie. Ho già provato questo. Ma questo non funziona. In questo caso la vista oggetto è una vista a schede. Il resto dei componenti è stato disegnato su un layout relativo – Ahmed

+0

Hmm, non sono sicuro di come risolverlo: -/Forse onClickListener non è l'ascoltatore che stai cercando ... Provare con -> ITEM <- ClickListener() Un'altra soluzione potrebbe essere: scrivere l'ascoltatore direttamente dietro la vista xy.setOnItemClickListener (new OnClickListener .../onItemClickListener ...) –

risposta

13

Quindi ho trovato il problema. I seguenti due tag sono stati i colpevoli qui

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

Quando abbiamo deciso attivabile e focusableInTouchMode = true fondamentalmente vuol dire che si sta abilitando fini della loro prima messa a fuoco su un touch e quindi essere in grado di fare clic.

+0

Hey Ahemd Sto avendo lo stesso identico problema. Così ha fatto impostare 'android: focusable =" true " android: focusableInTouchMode =" true "' to = "false"? - grazie – user1446988

+0

Sì. È corretto – Ahmed

+0

Lmk se questo funziona per te – Ahmed

3

Ho avuto un problema simile, ma la soluzione di impostazione messa a fuoco su false non ha funzionato nel mio caso.

Invece, il mio problema era quello che avevo impostato il listener onItemTouch nell'attività che gestiva la mia vista del riciclatore.

recyclerView.addOnItemTouchListener(...); 

Rimuovendo questo dalla mia attività, i miei altri ascoltatori di tocco hanno iniziato a rispondere su singoli tocchi e non solo il doppio.