OnItemCLickListener no funciona en la vista de lista

5 minutos de lectura

OnItemCLickListener no funciona en la vista de lista
hola

Activity código de clase:

conversationList = (ListView)findViewById(android.R.id.list);
ConversationArrayAdapter conversationArrayAdapter=new  ConversationArrayAdapter(this, R.layout.conversation_list_item_format_left, conversationDetails);
conversationList.setAdapter(conversationArrayAdapter);
conversationList.setOnItemClickListener(new AdapterView.OnItemClickListener(){ 
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
        Log.d("test","clicked");
    }
});

los getView función en el Adapter clase:

if (v == null) {                                
    LayoutInflater vi = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if(leftSideMessageNumber.equals(m.getTo())) {
        v = vi.inflate(R.layout.conversation_list_item_format_left, null);
    } else {
        v = vi.inflate(R.layout.conversation_list_item_format_right, null);
    }
}

¿Hay algún problema con el uso de dos xmls mientras se infla?

  • @hiei Mi problema era que estaba usando botones de imagen en mis celdas de diseño. Incluso después de agregar “android:descendantFocusability”, mi oyente de clics no respondía. Lo que hice fue cambiar todos los ImaegButtons a ImageViews. Eso resolvió mi problema.

    – Ajith Memana

    19 de noviembre de 2015 a las 7:43

OnItemCLickListener no funciona en la vista de lista
Bhavesh Hirpara

Acabo de encontrar una solución desde aquí, pero haciendo clic profundo.

Si algún elemento de la fila de la lista contiene una vista enfocable o en la que se puede hacer clic, entonces OnItemClickListener no funcionará

El elemento de la fila debe tener un parámetro como
android:descendantFocusability = "blocksDescendants".

Aquí puede ver un ejemplo de cómo debería verse el elemento de su lista. Su elemento de lista xml debe ser…
row_item.xml (your_xml_file.xml)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:descendantFocusability="blocksDescendants"
    android:gravity="center_vertical" >

    // your other widgets here

</LinearLayout>

  • no funciona para mi ¿Alguien puede echar un vistazo a [stackoverflow.com/questions/21692209/…

    – suitianshi

    Feb 11, 2014 at 3:07


  • You can also set this programatically with listView.setDescendantFocusability(int focus); where focus is one of ViewGroup.FOCUS_BEFORE_DESCENDANTS, ViewGroup.FOCUS_AFTER_DESCENDANTS or ViewGroup.FOCUS_BLOCK_DESCENDANTS

    – Max Worg

    Jan 31, 2015 at 13:03

  • I think that this is funny that like 20 people have thanked you in a comment. I have been running into this problem for the past 8 hours, so many thanks for this.

    – sparticvs

    Apr 28, 2015 at 21:15

  • My question is this is an android bug or is it by design?

    – fangzhzh

    Oct 19, 2015 at 9:21

  • I have been stuck on this for 6 hours now, and I really need help. stackoverflow.com/questions/35108940/why-cant-i-remove-an-item/…

    – Ruchir Baronia

    Jan 31, 2016 at 2:56

OnItemCLickListener no funciona en la vista de lista
balazsbalazs

The problem is that your layouts contain either focusable or clickable items.
If a view contains either focusable or clickable item the OnItemCLickListener won’t be called.

Click here for more information.

Please post one of your layout xmls if that isn’t the case.

  • This was the solution for me, i was declaring my view as clickable. Thanks!

    – kingraam

    Mar 6, 2012 at 13:27

  • Thanks! I changed my 2 EditTexts to TextViews, and the CheckBox was allowed to stay (and remain toggle-able) with android:descendantFocusability="blocksDescendants" added to my LinearLayout.

    – Azurespot

    May 13, 2016 at 3:27

  • Yes! I had android:focusableInTouchMode="true" for the row, once removed it started working at last! 🙂

    – SharpC

    Sep 18, 2016 at 10:21

1646757552 187 OnItemCLickListener no funciona en la vista de lista
Janene Pappas

For my lists, my rows have other things that can be clicked, like buttons, so doing a blanket blocksDescendants doesn’t work. Instead I add a line in the button’s xml:

    android:focusable="false"

That keeps the buttons from blocking the clicks on the rows, but still lets the buttons take the clicks, too.

  • @user1840899 This is a common response and one I learned from, so I passed it on. It is correct information, not worthy of down-voting.

    – Janene Pappas

    May 30, 2013 at 17:32

  • This works for me on a CheckBox in ListView items (without blocksDescendants)

    – thpitsch

    Jul 25, 2013 at 4:19

  • This fixes the bug while keeping the buttons (ToggleButton for me) clickable

    – Mohsen Afshin

    Aug 6, 2013 at 8:51

  • Only way I could get a GridView with RadioButtons in it working was to apply android:descendantFocusability=”blocksDescendants” on the LinearLayout containing the RadioButton, and by setting android:clickable=”false” in the RadioButton itself.

    – Stephen McCormick

    Oct 16, 2015 at 20:32

  • I have been stuck on this for 6 hours now, and I really need help. stackoverflow.com/questions/35108940/why-cant-i-remove-an-item/…

    – Ruchir Baronia

    Jan 31, 2016 at 2:57

you need to do 2 steps in your listview_item.xml

  1. set the root layout with: android:descendantFocusability="blocksDescendants"
  2. set any focusable or clickable view in this item with:
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false"

Here is an example: listview_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:layout_marginTop="10dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:gravity="center_vertical"
    android:orientation="vertical"
    android:descendantFocusability="blocksDescendants">

    <RadioButton
        android:id="@+id/script_name_radio_btn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textColor="#000"
        android:padding="5dp"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        />

</LinearLayout>

use the below code inside button tag in custom row layout of listview

 android:focusable="false"
 android:clickable="false"

  • Great Man its was really helpfull!!

    – divyansh ingle

    Feb 14, 2017 at 6:35

  • this worked here!! I have many checkbox in this view and I try with android:descendantFocusability="blocksDescendants" and block these checkbox for click

    – Armando Marques da S Sobrinho

    Mar 5, 2017 at 0:35


1646757553 800 OnItemCLickListener no funciona en la vista de lista
joecizac

I had the same problem and I just saw I had accidentally set:

@Override
public boolean isEnabled(int position)
{
    return false;
}

on my CustomListViewAdapter class.

By changing this to:

return true;

I’ve managed to fix the problem.
Just in case if someone has done the same mistake…

  • Great Man its was really helpfull!!

    – divyansh ingle

    Feb 14, 2017 at 6:35

  • this worked here!! I have many checkbox in this view and I try with android:descendantFocusability="blocksDescendants" and block these checkbox for click

    – Armando Marques da S Sobrinho

    Mar 5, 2017 at 0:35


1646757554 137 OnItemCLickListener no funciona en la vista de lista
Mahesh Babariya

Use android:descendantFocusability

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="80dip"
    android:background="@color/light_green"
    android:descendantFocusability="blocksDescendants" >

Add above in root layout

  • Adding details might make this a better answer and +1.

    – Shubham Agarwal

    Oct 2, 2017 at 5:56

  • This worked for me and my button in the list element still worked as intended.

    – Gustav Eriksson

    Mar 25, 2018 at 12:37

¿Ha sido útil esta solución?

Esta web utiliza cookies propias y de terceros para su correcto funcionamiento y para fines analíticos y para mostrarte publicidad relacionada con sus preferencias en base a un perfil elaborado a partir de tus hábitos de navegación. Al hacer clic en el botón Aceptar, acepta el uso de estas tecnologías y el procesamiento de tus datos para estos propósitos. Configurar y más información
Privacidad