3

我正在尝试为 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;
    }
}

我试过 set 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>

我不明白为什么这段代码不起作用!我该如何解决?

4

1 回答 1

13

你必须使用setOnItemLongClickListener

gallery.setOnItemLongClickListener(new OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
            final int arg2, long arg3) {

    });
}

此外,如果您的列表视图的适配器从 BaseAdapter 扩展,那么您还需要convertView.setLongClickable(true);getView().

于 2013-05-02T09:08:31.620 回答