gpt4 book ai didi

java - 长按不适用于 ListView

转载 作者:太空宇宙 更新时间:2023-11-03 11:37:39 25 4
gpt4 key购买 nike

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

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

最佳答案

你必须使用setOnItemLongClickListener

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

});
}

此外,如果您的 ListView 的适配器扩展自 BaseAdapter,那么您还需要在 getView() 中设置 convertView.setLongClickable(true);

关于java - 长按不适用于 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16334119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com