setOnItemClickListener does not work on custom Adapter

0

I have a custom adapter that is displayed in the 'ListView', works normally at the time of creating it, and so on. The problem is that I set the 'OnItemClickListener' but the method is never called. I searched and no answer how to get the 'android: clickable' from 'ListView', or use 'android: focusable' and 'android: focusableInTouchMode' as false worked (I even put it in all my layout items) p>

XML of the ListView item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/favoritos_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants"
android:padding="16dp">

<TextView
    android:id="@+id/indicador"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="Indicacao"
    android:textColor="@color/cor_texto_favoritos"
    android:textSize="22sp" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/favoritos_body_background"
    android:orientation="vertical">

    <TextView
        android:id="@+id/endereco"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:maxLength="80"
        android:text="@string/enderecoIndicador"
        android:textSize="16sp" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/preco"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="4dp"
            android:layout_marginLeft="4dp"
            android:paddingRight="22dp"
            android:text="@string/precoIndicador"
            android:textSize="16sp" />


        <TextView
            android:id="@+id/precoValor"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@id/preco"
            android:textSize="16sp" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/horaAbreTxt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="4dp"
            android:paddingRight="27dp"
            android:text="@string/horaAbre_indicador"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/horaAbre"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@id/horaAbreTxt"
            android:textSize="16sp" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/horaFechaTxt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="4dp"
            android:paddingRight="34dp"
            android:text="@string/horaFecha_indicador"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/horaFecha"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@id/horaFechaTxt"
            android:textSize="16sp" />

    </RelativeLayout>

    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:isIndicator="true"
        android:clickable="false"
        android:numStars="5"
        android:layout_gravity="center"
        android:rating="3"
        android:stepSize="0.2" />

</LinearLayout>

<LinearLayout
    android:id="@+id/disponivel_container"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/img_disponibilidade"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="left"
        android:src="@drawable/ir_img" />

    <TextView
        android:id="@+id/disponivel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="10dp"
        android:text="@string/aberto"
        android:textColor="@color/verde"
        android:textSize="20dp" />

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <ImageView
        android:id="@+id/opcoes_favoritos_item"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_margin="16dp"
        android:src="@drawable/opcoes_aberto" />
</LinearLayout>

</LinearLayout>

ListView XML

 <?xml version="1.0" encoding="utf-8"?>
 <ListView xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/list_item"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical"
     android:clickable="true"
     tools:context="project.parking.FavoritosFragment">
 </ListView>

Fragment that hedge the onItemClickListener

public ArrayList<Favoritos> favoritos;
public FavoritosAdapter mAdapter;
ListView listView;
EncontradosAdapter adapter;
Favoritos favoritosExcluidos;

public static final String KEY_FAVORITO = "FAVORITO";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View rootView = inflater.inflate(R.layout.list_item, container, false);

    mAdapter = new FavoritosAdapter(getActivity(), carregarFavoritos());

    listView = (ListView) rootView.findViewById(R.id.list_item);
    listView.setItemsCanFocus(false);

    listView.setAdapter(mAdapter);

    listView.setItemsCanFocus(false);
    registerForContextMenu(listView);

    mAdapter.notifyDataSetChanged();

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Favoritos favorito = favoritos.get(position);
            Intent intent = new Intent(getContext(), InfoActivity.class);
            intent.putExtra(KEY_FAVORITO, favorito);
            startActivity(intent);
        }
    });

    return rootView;
}

public ArrayList<Favoritos> carregarFavoritos() {
    favoritos = new ArrayList<Favoritos>();
    favoritos.add(new Favoritos("indicacao", "endereco", 5, false, 7, 22, 4.5f));
    favoritos.add(new Favoritos("indicacao", "endereco", 3.5, true, 7, 22, 3.0f));
    favoritos.add(new Favoritos("indicacao", "endereco", 2, false, 7, 22, 4.0f));
    favoritos.add(new Favoritos("indicacao", "endereco", 4.5, true, 7, 22, 5.0f));
    return favoritos;
}

public void buscar(String s) {
    if (s == null || s.trim().equals("")) {
        limparBusca();
        return;
    }

    ArrayList<Favoritos> estacionamentosEncontrados = new ArrayList<Favoritos>(favoritos);

    for (int i = estacionamentosEncontrados.size() - 1; i >= 0; i--) {
        Favoritos estacionamento = estacionamentosEncontrados.get(i);
        Favoritos nenhumResultado = new Favoritos("Nenhum resultado encontrado");
        if (!estacionamento.mIndicacao.toUpperCase().contains(s.toUpperCase())) {
            estacionamentosEncontrados.remove(estacionamento);
        } else {
            estacionamentosEncontrados.add(nenhumResultado);
        }
    }

    adapter = new EncontradosAdapter(getActivity(), estacionamentosEncontrados);
    ListView listView = (ListView) getActivity().findViewById(R.id.list_item);
    listView.setAdapter(adapter);
}

public void limparBusca() {
    mAdapter = new FavoritosAdapter(getActivity(), favoritos);
    listView.setAdapter(mAdapter);
}

I'm using a 'ViewHolder' in the 'Adapter'. Thank you in advance!

    
asked by anonymous 19.10.2017 / 02:18

1 answer

1

When the layout of ListView items contains Views that react to "clicks" or receive focus , the "click", made available by listView.setOnItemClickListener , is disabled.

To enable it, you must include android:descendantFocusability="blocksDescendants" at the highest level of the item layout hierarchy.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/favoritos_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical"
    android:padding="16dp"

    android:descendantFocusability="blocksDescendants">

    ...
    ...

</LinearLayout>
    
19.10.2017 / 15:53