如何在Android的listview行上实现onItemLongClickListener和onItemClickListener事件? [英] How to implement onItemLongClickListener and onItemClickListener event on listview row on Android?
问题描述
我正在尝试在列表视图行上实现onItemLongClickListener
和onItemClickListener
事件,但是问题是,当我长按列表视图行并释放它时,这两个事件将同时被调用.实现该目标的解决方案是什么.
I am trying to implement the onItemLongClickListener
and onItemClickListener
events on a listview row, but the problem is that when I longPress the listview row and release it then both events get called at the same time. What would be the solution that achieves this.
这是我正在使用的代码.
Here is the code I am using.
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;
}
});
推荐答案
尝试一下;它会工作.我注意到您在listvideos.setOnItemLongClickListener
中返回false.而是返回true.
Try this; it will work. I noticed that you are returning false in listvideos.setOnItemLongClickListener
. Instead return true.
原因::在执行onItemLongClick之后返回true可以防止在onItemLongClick之后触发onItemClick事件.例如,
Reason: Returning true after performing onItemLongClick prevents firing your onItemClick event after onItemLongClick. For example,
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;
}
});
这篇关于如何在Android的listview行上实现onItemLongClickListener和onItemClickListener事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!