android – 如何将OnTouchListener添加到listview项目?

我正在编写一个简单的
Android应用程序,其中包含一个填充了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);

}

列表和list_row的xml:

<?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"
    /> 

此外,我在listview上设置了一个OnTouchListener来监听正常工作的滑动和点击事件,除了它不会侦听列表中任何项目中发生的事件.做一些研究我发现我需要扩展SimpleCursorAdapter来为所有项添加监听器.这是我的扩展课程:

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

这不起作用.有没有人知道如何使它工作?

编辑:只是为了澄清,我想要的是识别列表项目上的手势(向左滑动,向右滑动,单击).到目前为止,如果我在列表的空白部分(在项目下)执行手势,则侦听器会捕获事件,但如果我在某个项目上执行此操作,则无法识别滑动.

最佳答案 我在问题中发布的代码工作正常.问题出在我的触摸侦听器中,它没有执行手势事件请求的操作.

点赞