Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement onItemLongClickListener and onItemClickListener event on listview row on Android?

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;
    }
});
like image 414
rahul Avatar asked Dec 04 '22 09:12

rahul


1 Answers

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

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

EDIT: Change your code as follows.

Your earlier 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;
    }
});
like image 100
Ritesh Gune Avatar answered May 15 '23 04:05

Ritesh Gune