How to implement onItemLongClickListener and onItemClickListener event in a list bar on Android?

I am trying to implement the onItemLongClickListener and onItemClickListener on a list line, but the problem is that when I click on the list line for a long time and release it, both events are fired simultaneously. What will be the solution to achieve this.

Here is the code I'm 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; } }); 
+4
source share
4 answers

Try it; it will work. I noticed that you are returning false in listvideos.setOnItemLongClickListener . Instead, return true.

Reason: Returning true after running onItemLongClick prevents your onItemClick event from firing after onItemLongClick. For instance,

 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { return true; } 

EDIT: change the code as follows.

Your previous code:

 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; } }); 

Change it to:

 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; } }); 
+10
source

I found that the trick is in the return value of the longclick listener call. If you return true, onclick will not be called after the longclick call, and a simple click will only be called when clicked. try this option and let me know.

+5
source

Try the following:

  // 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); } 
+1
source

Put your listvideos.setOnItemLongClickListener() before listvideos.setOnItemClickListener() .

Thus, when you click on an element for a long time, it will not execute onItemClickListener() .

+1
source

Source: https://habr.com/ru/post/1497584/


All Articles