我正在尝试为ListView设置长按侦听器:
final ListView gallery=(ListView)findViewById(R.id.dialogViewImagesList);
gallery.setLongClickable(true);
gallery.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View arg0) {
Log.e("event", "long");
return true;
}
});
gallery.setAdapter(new PointImagesAdapter(bitmaps));
这是我的适配器:
private class PointImagesAdapter extends ArrayAdapter<Bitmap> {
private static final int LAYOUT_ID=R.layout.adapter_point_images;
private List<Bitmap> bitmaps;
private LayoutInflater inflater;
public PointImagesAdapter(List<Bitmap> bitmaps) {
super(MainActivity.this, LAYOUT_ID, bitmaps);
this.bitmaps=bitmaps;
inflater=LayoutInflater.from(MainActivity.this);
}
@Override
public View getView(int position, View view, ViewGroup group) {
if (view==null) {
view=inflater.inflate(LAYOUT_ID, null);
}
ImageView i=(ImageView)view.findViewById(R.id.adapterPointImagesItem);
i.setScaleType(ImageView.ScaleType.CENTER);
i.setImageBitmap(bitmaps.get(position));
view.setFocusable(false);
return view;
}
}
我尝试过设置view.setLongClickable(true),但在这种情况下,ListView项目不可点击(简单点击不起作用)。它是适配器的布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="false"
android:orientation="vertical" >
<ImageView
android:focusable="false"
android:layout_gravity="center"
android:layout_marginTop="5dip"
android:id="@+id/adapterPointImagesItem"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
我不明白为什么这段代码不起作用!我该如何解决?
答案 0 :(得分:13)
您必须使用setOnItemLongClickListener
gallery.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
});
}
此外,如果listview的适配器从BaseAdapter扩展,那么您还需要在convertView.setLongClickable(true);
中设置getView()
。