ListView同时实现点击和长按事件

时间:2021-12-13 15:50:28

最近在做项目中,要在同一个listview中同一个item同时实现点击和长按事件,

点击代码如下:


/**
* click lister
*/
private OnItemClickListener itemClickListener = new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
长按:

private OnItemLongClickListener itemLongClickListener = new OnItemLongClickListener() {

@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Intent intent = new Intent(BindingActivity.this,DialogBinding.class);
intent.putExtra("type", DialogStyle.ONE);
startActivity(intent);
Log.i(TAG, "长按了哦!");
return false;
}
};

运行后发现当长按之后长按的事件和点击的事件都执行了,查了一下网上大神的解释:

itemLongClickListener返回false时就会触发list view的点击事件,所以吧返回的false改为true即可

return true;