我正在尝试在listview行上实现onItemLongClickListener
和onItemClickListener
事件,但问题是当我longPress listview行并释放它时,两个事件同时被调用。实现这一目标的解决方案是什么。
这是我正在使用的代码。
listvideos.setLongClickable(true);
listvideos.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,int pos, long arg3) {
System.out.println("hh clickkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk");
if( lastLoded == TIMELINE || lastLoded == UPLOADS){
Intent i = new Intent(getActivity(), VideoStreamingActivity.class);
i.putExtra("clipname", videosVo.getInnerTopVideosVos().get(pos).getClipName());
i.putExtra("clipurl", videosVo.getInnerTopVideosVos().get(pos).getClipUrl());
i.putExtra("uploadername", videosVo.getInnerTopVideosVos().get(pos).getUploader_name());
i.putExtra("clipid", videosVo.getInnerTopVideosVos().get(pos).getClipId());
i.putExtra("rating", videosVo.getInnerTopVideosVos().get(pos).getRating());
i.putExtra("views", videosVo.getInnerTopVideosVos().get(pos).getTotalViews());
i.putExtra("thumburl", videosVo.getInnerTopVideosVos().get(pos).getThumbUrl());
adapterTopvideos.increaseViews(pos);
startActivity(i);
}
else if(lastLoded == PROFILE){
Intent i = new Intent(getActivity(), FriendProfileActivity.class);
i.putExtra("friendid", videosVo.getInnerFriendsVos().get(pos).getId());
i.putExtra("friendname", videosVo.getInnerFriendsVos().get(pos).getName());
ApplicationConstants.bmpFriend = videosVo.getInnerFriendsVos().get(pos).getImage();
startActivity(i);
}
}
});
listvideos.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long arg3) {
System.out.println("hh longgggggggggggggggggggggggggggg click");
// if(lastLoded == UPLOADS){
//
// if(!videosVo.getInnerTopVideosVos().get(pos).isChecked())
// videosVo.getInnerTopVideosVos().get(pos).setChecked(true);
// else
// videosVo.getInnerTopVideosVos().get(pos).setChecked(false);
//
// adapterTopvideos.notifyDataSetChanged();
// }
return false;
}
});
答案 0 :(得分:10)
试试这个;它会工作。我注意到你在listvideos.setOnItemLongClickListener
中返回了false。而是返回true。
原因:执行onItemLongClick后返回true会阻止在onItemLongClick之后触发onItemClick事件。例如,
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
return true;
}
编辑:更改您的代码,如下所示。
您之前的代码:
listvideos.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long arg3) {
System.out.println("hh longgggggggggggggggggggggggggggg click");
// if(lastLoded == UPLOADS){
//
// if(!videosVo.getInnerTopVideosVos().get(pos).isChecked())
// videosVo.getInnerTopVideosVos().get(pos).setChecked(true);
// else
// videosVo.getInnerTopVideosVos().get(pos).setChecked(false);
//
// adapterTopvideos.notifyDataSetChanged();
// }
return false;
}
});
将其更改为:
listvideos.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long arg3) {
System.out.println("hh longgggggggggggggggggggggggggggg click");
// if(lastLoded == UPLOADS){
//
// if(!videosVo.getInnerTopVideosVos().get(pos).isChecked())
// videosVo.getInnerTopVideosVos().get(pos).setChecked(true);
// else
// videosVo.getInnerTopVideosVos().get(pos).setChecked(false);
//
// adapterTopvideos.notifyDataSetChanged();
// }
return true;
}
});
答案 1 :(得分:4)
我发现诀窍在于longclick侦听器回调的返回值。如果返回true,则在调用longclick后将不会调用onclick,并且只有在单击时才会调用simple click。 请尝试这个,让我知道。
答案 2 :(得分:1)
试试这个:
// Item Click Listener for the listview
OnItemLongClickListener itemClickListener = new OnItemLongClickListener() {
@SuppressWarnings("unchecked")
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
HashMap<String, Object> item=(HashMap<String, Object>) arg0.getItemAtPosition(arg2);
switch (arg2) {
// Set event handler
case 0:
break;
case 1:
break;
case 2:
break;
....
}
}
};
// Setting the item click listener for the listview
listView.setOnItemLongClickListener(itemClickListener);
}
答案 3 :(得分:1)
将listvideos.setOnItemLongClickListener()
放在listvideos.setOnItemClickListener()
之前。
这样,当您长按项目时,它将无法执行onItemClickListener()
。