Items in ListView not long clickable after setting click listener in getView()

自古美人都是妖i 提交于 2019-12-10 14:36:57

问题


I've searched around and have not come out with a solution (maybe not using the correct keywords).

So, I've a custom ListView which its item can be dragged around when the item is long clicked. Within its item, there's an ImageView and LinearLayout containing two TextViews. Actions are done when the LinearLayout or ImageView is clicked.

To do this, I've use setOnItemLongClickListener on my DragListView which extends ListView, to initiate drag action, and onInterceptTouchEvent to manage the drag action.

Then, I have built a custom adapter extending BaseAdapter and overrided its getView() to implement the child items in the row. The LinearLayout and ImageView have been setOnClickListener.

The problem is, the LinearLayout and ImageView are able to do their stuff, but the onItemLongClick isn't called.

The listener inside getView();

    holder.delete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
//Do something
}

For item long click (drag initiator)

setOnItemLongClickListener(new OnItemLongClickListener() {              

    @Override
                public boolean onItemLongClick(AdapterView<?> parent, View view,
                        int position, long id) {
    //Do something
    }

Thank you very much!


回答1:


I think that a gesture detector is one of ways to handle events. Usually, however, a gesture detector is used when we want to detect a gesture not a long-press.

The reason why onItemLongClick isn't called is that onClickListener might consume a touch event. In that reason, if you want to handle onItemLongClick, intercept touch event and dispatch it to views you want to handle.

You can find more details following link. http://developer.android.com/guide/topics/ui/ui-events.html




回答2:


Ok, just found out the solution myself. Instead of using onItemLongClickListener, I create a gesture detector to detect for long press. Then I override dispatchTouchEvent and force to scan for long press first, then return super.dispatchTouchEvent and the other following touch events. Suggestions are still welcomed!



来源:https://stackoverflow.com/questions/5855541/items-in-listview-not-long-clickable-after-setting-click-listener-in-getview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!