setOnItemClickListener does not work in Custom Adapter

Asked

Viewed 265 times

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 have 'Onitemclicklistener' but the method is never called. I searched and no answer how to take the 'android:clickable' from 'Listview', or use 'android:focusable' and 'android:focusableInTouchMode' as false worked (I even put this in all the items of my layout).

Listview entry XML'

<?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 Seto 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 am using a 'Viewholder' in the 'Adapter'. Thanks in advance!

  • 1

    Where is the code that assigns the OnItemClickListener ?

  • A doubt. you are wanting to set up a menu?

  • Pq use this registerForContextMenu(listview);?

  • yes, I already have a menu. if you want I can include in the code there

  • Just to validate.. try using android:focusable = "false" android:focusableInTouchMode = "false" on all clickable components.. as Imageview for example..

  • put on all Imageviews, Textviews and Ratingbar. It didn’t work

Show 1 more comment

1 answer

2


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

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

<?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>
  • It didn’t work. I also tried to take android:focusable="false" and android:focusableInTouchMode="false" when I declared it but nothing happened either

  • It has to work. Remove all android:focusable="false" e android:focusableInTouchMode="false" because they are not necessary. Remove the android:onClick="opcoesEstacionamento" because it will have to be treated in the Adapter.

  • I didn’t test with its full layout, but tested with a set of Textview’s and a Ratingbar (which is what I think is creating the problem).

  • edited the code up there. implemented correctness?

  • added also the search tool you have there. that’s what might be causing the problem?

  • This seems to be another list that is in Activity and not in Fragment.

  • I commented the whole code related to the search, but it still didn’t work

  • Do you have two lists or not? If so which one are you clicking on? If you are clicking on the one that is not in the Fragment it clearly does not work.

  • No, it’s the same listview that’s declared global

  • What do you mean? You are creating another list in the method buscar(String s) with ListView listView = (ListView) getActivity().findViewById(R.id.list_item);

  • It is because the original Adapter displayed a bit large items, then at the time of searching it was bad to see, so I created a different layout for the search item

  • Which one are you clicking on? The click code(both the setOnItemClickListener like the android:descendantFocusability="blocksDescendants" must be applied to Listview and Layout where you are clicking.

  • I applied to Listview too. Nothing happened

  • How I apply setOnItemClickListener in Linearlayout?

  • My goal is to open an Activity by passing the item data according to the position. If you have any way to do the same thing without using setOnItemClickListener can also be

  • and one more question my... because the setOnItemLongClickListener works and the setOnItemClickListener does not?

  • 1

    I managed to solve it! Your help was fundamental, the error was being with myself. My class extended Listfragment and not Fragment

Show 12 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.