我正在编写一个简单的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;
}
}
这不起作用。有人知道如何使其工作吗?
编辑:只是想澄清,我希望能够识别在列表项上执行的手势(向左滑动、向右滑动、点击)。到目前为止,如果我在列表的空白部分(项下方)执行手势,监听器会捕获事件,但如果我在一个项目上执行手势,则无法识别滑动。