简体   繁体   中英

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

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

I found that the trick is in the return value of the longclick listener call back. If you return true, onclick will not be called after the longclick is called and simple click will invoke only on click. please try this one and let me know.

Try this:

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

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

That way when you long click on the item it will not perform the onItemClickListener() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM