android - 如何将 OnTouchListener 添加到 ListView 项?

标签 android listview gesture simplecursoradapter

我正在编写一个简单的 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 以将监听器添加到所有项目。这是我的扩展类(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;
    }
}

这是行不通的。有谁知道如何让它发挥作用吗?

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

最佳答案

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

关于android - 如何将 OnTouchListener 添加到 ListView 项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9802221/

相关文章:

java - 双指缩放仅放大到中心

ios - 为什么 MagnificationGesture 在 SwiftUI 中只更新一次状态?

android - 向 ListView 添加参数

Android sqlite3_limit?

java - 从另一个类调用 Facebook GraphRequest 返回 null

java - 为什么 findViewById 返回 null?

java - 在 Android Studio 中使用 SharedPreferences 保存列表

listview - 将 View 模型绑定(bind)到 ListView

iphone开发-UItapGestureRecognizer(顺时针或逆时针检测)?

android - 使用 appium-python-client 在 android 中缩放 Action