Android longClick在我的应用程序中不起作用
我有一个带有Custon适配器的listview。我为longclick实现了一个监听器来处理上下文菜单。问题是长时间的点击不起作用。上下文菜单不显示 代码如下: listview中的侦听器Android longClick在我的应用程序中不起作用,android,android-listview,contextmenu,android-adapter,android-contextmenu,Android,Android Listview,Contextmenu,Android Adapter,Android Contextmenu,我有一个带有Custon适配器的listview。我为longclick实现了一个监听器来处理上下文菜单。问题是长时间的点击不起作用。上下文菜单不显示 代码如下: listview中的侦听器 registerForContextMenu(lv); lv.setOnItemLongClickListener(new OnItemLongClickListener() { @Override public boolean onItemLongClic
registerForContextMenu(lv);
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
seguro = (SeguroSelecaoModel)adapter.getItem(position);
return false;
}
});
<ListView
android:id="@+id/lvSeguroPrincipal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/imgFaixaSeguro2"
android:layout_alignParentLeft="true"
android:layout_below="@+id/imgFaixaSeguro"
android:divider="@android:color/transparent"
android:longClickable="true" >
上下文菜单中的代码
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle(seguro.getNomeSeguradora());
menu.add("Selecionar Seguro");
menu.add("Excluir Seguro");
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
sDAO.open();
if(item.getTitle().equals("Excluir Seguro")){
sDAO.selecionaSeguro(seguro.getId());
adapter.clearList();
buildList();
sDAO.close();
} else if(item.getTitle().equals("Selecionar Seguro")){
sDAO.removeSeguro(seguro.getId());
}
return super.onMenuItemSelected(featureId, item);
}
来自listview的xml
registerForContextMenu(lv);
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
seguro = (SeguroSelecaoModel)adapter.getItem(position);
return false;
}
});
<ListView
android:id="@+id/lvSeguroPrincipal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/imgFaixaSeguro2"
android:layout_alignParentLeft="true"
android:layout_below="@+id/imgFaixaSeguro"
android:divider="@android:color/transparent"
android:longClickable="true" >
如果使用setOnItemLongClickListener,它应该返回true。看
lv.setOnItemLongClickListener(新的OnItemLongClickListener(){
@凌驾
公共布尔值长单击(AdapterView父对象、视图v、整型位置、长id){
seguro=(seguroselecomodel)适配器.getItem(位置);
返回true;
}
});
您必须为activity onCreate方法上的上下文菜单注册listView:
registerForContextMenu(lv);
您不需要这一行view.setLongClickable(true)适配器上的代码>
而且,在这种特殊情况下,setOnItemLongClickListener
应该返回false
长点击不起作用。这是为了显示上下文菜单。但是它没有发生。我做了这个registerForContextMenu(lv)区域。它不工作。您是否已删除view.setLongClickable(true)在适配器上单击code>,然后在设置OnItemLongClickListener上放回return false
?在OnCreateContext菜单上,必须为menuI打开视图。我在适配器上设置view.setLongClickable(true),并让侦听器返回false。我不知道这是什么原因,因为我在另一个类中有相同的代码,但没有这个设置LongClickable(true),并且在另一个类中它工作了。但不管怎样,还是泰克人。看看这篇文章,看看如何