Search code examples
androidlistviewonlongclicklistener

OnItemLongClickListener too long. How to listen for a bit shorter click in listview?


I use the drag & drop list view from this tutorial :http://www.youtube.com/watch?v=_BZIvjMgH-Q. With this, we can drag items of the list after a long click, but's it's too long.

The code :

 /**
     * Listens for long clicks on any items in the listview. When a cell has
     * been selected, the hover cell is created and set up.
     */
    private AdapterView.OnItemLongClickListener mOnItemLongClickListener = new AdapterView.OnItemLongClickListener() {
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos, long id) {
        //code
        }
    };

I tryed to set "onItemClickListener", but after I need to click once to focus the item and click twice to drag it.

How can I replace this code to have a long click effect shorter ? Thanks for your help


Solution

  • I have found a solution. I used a custom adapter for my list to listen each item, and I have been created a listener onLongCustomItemClick using the pattern observer.

    code of the getView of the adapter :

    @Override
        public View getView(int pPosition, View pConvertView, ViewGroup pParent) {
            ViewHolder holder = null;
            final int posCopy = pPosition;
            if (pConvertView == null) {
                LayoutInflater inflater = listViewDraggingAnimation.getLayoutInflater();
                pConvertView = inflater.inflate(R.layout.text_view, null);
                holder = new ViewHolder();
                holder.tvTitle = (TextView) pConvertView.findViewById(R.id.title);
                pConvertView.setOnTouchListener(new OnTouchListener() {
    
                    boolean mHasPerformedLongPress;
                    Runnable mPendingCheckForLongPress;
    
                    @Override
                    public boolean onTouch(final View v, MotionEvent event) {
                        switch (event.getAction()) {
                        case MotionEvent.ACTION_UP:
                            if (!mHasPerformedLongPress) {
                                // This is a tap, so remove the longpress check
                                if (mPendingCheckForLongPress != null) {
                                    v.removeCallbacks(mPendingCheckForLongPress);
                                }
                                // v.performClick();
                            }
                            break;
                        case MotionEvent.ACTION_DOWN:
                            if (mPendingCheckForLongPress == null) {
                                mPendingCheckForLongPress = new Runnable() {
                                    public void run() {
                                        updateOnItemCustomLongClickListener();
                                    }
                                };
                            }
                            mHasPerformedLongPress = false;
                            v.postDelayed(mPendingCheckForLongPress, timeForLongClick);
                            break;
                        case MotionEvent.ACTION_MOVE:
                            final int x = (int) event.getX();
                            final int y = (int) event.getY();
    
                            // Be lenient about moving outside of buttons
                            int slop = ViewConfiguration.get(v.getContext()).getScaledTouchSlop();
                            if ((x < 0 - slop) || (x >= v.getWidth() + slop) || (y < 0 - slop)
                                    || (y >= v.getHeight() + slop)) {
    
                                if (mPendingCheckForLongPress != null) {
                                    v.removeCallbacks(mPendingCheckForLongPress);
                                }
                            }
                            break;
                        default:
                            return false;
                        }
    
                        return false;
                    }
                });
                pConvertView.setTag(holder);
                pConvertView.setTag(R.id.title, holder.tvTitle);
            } else {
                holder = (ViewHolder) pConvertView.getTag();
            }
            holder.tvTitle.setText(optionList.get(pPosition).getTitle());
            return pConvertView;
    

    Here updateOnItemCustomLongClickListener(); correspond to the update of the observer which execute an override method onCustomLongClick (generated by the interface of my pattern observer) which replace AdapterView.OnItemLongClickListener at the top of the topic. timeForLongClick is the long click time.