How to implement onItemLongClickListener and onIte

2020-07-17 04:06发布

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

4条回答
Rolldiameter
2楼-- · 2020-07-17 04:53

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.

查看更多
爷的心禁止访问
3楼-- · 2020-07-17 04:58

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

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

查看更多
趁早两清
4楼-- · 2020-07-17 05:05

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;
    }
});
查看更多
Ridiculous、
5楼-- · 2020-07-17 05:11

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);
}
查看更多
登录 后发表回答