如何为ListView的项添加OnTouchListener?

4

我正在编写一个简单的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);

}

列表和列表行的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;
    }
}

这不起作用。有人知道如何使其工作吗?

编辑:只是想澄清,我希望能够识别在列表项上执行的手势(向左滑动、向右滑动、点击)。到目前为止,如果我在列表的空白部分(项下方)执行手势,监听器会捕获事件,但如果我在一个项目上执行手势,则无法识别滑动。


不清楚...你想要什么? - Code_Life
https://dev59.com/AG855IYBdhLWcg3wuG0M - Karthi
1个回答

1

我在问题中发布的代码运行良好。问题出在我的触摸监听器上,它没有执行手势事件所请求的操作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接