Reputation: 1121
I'm trying to set long-click 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));
It's 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've tried set view.setLongClickable(true), but in this case ListView items are not clickable (simple click doesn't work). It's layout code for 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 fix it?
Upvotes: 3
Views: 3069
Reputation: 10553
You have to use setOnItemLongClickListener
gallery.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
});
}
Also, If your listview's adapter extends from BaseAdapter, then you also need to set convertView.setLongClickable(true);
in the getView()
.
Upvotes: 13