gpt4 book ai didi

android - 如何用 OnItemLongClickListener 覆盖 OnItemClickListener

转载 作者:太空狗 更新时间:2023-10-29 14:05:06 27 4
gpt4 key购买 nike

我的 ListView Activity 中有 OnItemClickListener,我在其中开始另一个 Activity ,我还有 OnItemLongClickListener,基本上我想删除长按的行。当我单击 ListView 时,OnItemClickListener 工作正常,但是当我长按列表仍然是 OnItemClickListener 和 OnItemLongClickListener。我不希望 OnItemClickListener 在我长按时工作。这就是我的代码的样子。

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, CompleteInformationActivity.class);
intent.putExtra("position", "" + selected.get(position));
startActivity(intent);
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "Long click happened.", Toast.LENGTH_SHORT).show();
adapter.notifyDataSetChanged();
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Do you want to delete?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "Item is Deleted.", Toast.LENGTH_SHORT).show();

}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "Item is not Deleted.", Toast.LENGTH_SHORT).show();

}
});

AlertDialog dialog = builder.create();
dialog.show();
return false;
}
});

最佳答案

我认为您应该在 onItemLongClick 方法中返回 true 以消耗点击。查看 Android 文档 http://developer.android.com/reference/android/widget/AdapterView.OnItemLongClickListener.html

关于android - 如何用 OnItemLongClickListener 覆盖 OnItemClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33107829/

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