Java – long press is not applicable to listview

I'm trying to set up a long press listener for 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));

This is my adapter:

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;
    }
}

I tried setting view Setlongclickable (true), but in this case, the listview item is not clickable (simple clicking does not work) It is the layout code of the adapter:

<?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>

I don't understand why this code doesn't work! How can I solve it?

Solution

You must use setonitemlongclicklistener

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

    });
}

In addition, if the adapter of listview is extended from baseadapter, you also need to set convertview setLongClickable(true); In getview()

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>