How to add OnTouchListener to list items?

I am coding a simple Android application that contains a list populated by SimpleCursorAdapter.

private void populateList() { c = this.cDAO.fetchAllContacts(); startManagingCursor(c); String[] from = new String[]{ContactsDAO.KEY_NOME}; int[] to = new int[]{R.id.nome1}; SimpleCursorAdapter notes = new MyListAdapter(this, R.layout.list_row, c, from, to, activitySwipeDetector); setListAdapter(notes); } 

xml for list and list_row:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listaBottom" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#B5E61D" > <ListView android:id="@+id/android:list" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#B5E61D" android:divider="#80FFFF" android:dividerHeight=".5dp" /> </LinearLayout> 

 <?xml version="1.0" encoding="utf-8"?> <TextView android:id="@+id/nome1" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="30dp" android:textColor="#5289DC" /> 

In addition, I set OnTouchListener to a listview to listen to the scroll and clicks of events that work correctly, except that it does not listen to events that occur in any of the elements in the list. After doing some research, I found that I needed to extend the SimpleCursorAdapter to add a listener to all the elements. Here is my extended class:

 public class MyListAdapter extends SimpleCursorAdapter { ActivitySwipeDetector asd; public MyListAdapter(Context context, int layout, Cursor c, String[] from, int[] to, ActivitySwipeDetector asd) { super(context, layout, c, from, to); this.asd = asd; } public View getView(int position, View convertView, ViewGroup parent) { View view = super.getView(position, convertView, parent); TextView row = (TextView)view.findViewById(R.id.nome1); row.setOnTouchListener(asd); return view; } } 

This does not work. Does anyone have an idea on how to make it work?

EDIT: just to clarify, all I want is that gestures made in list items (swipe left, swipe right, click). So far, if I make a gesture in the empty part of the list (under the elements), the listener catches this event, but if I do it by the element, the swipe is not recognized.

+4
source share
1 answer

The code posted in the question is working fine. The problem was in my touch listener, which did not perform the actions requested by the gesture events.

+2
source

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


All Articles