OnItemLongClickListener 太长.如何在列表视图中收听较短的点击



我使用本教程中的拖放列表视图:http://www.youtube.com/watch?v=_BZIvjMgH-Q。有了这个,我们可以在长时间单击后拖动列表中的项目,但它太长了。

代码 :

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

我尝试设置"onItemClickListener",但是在我需要单击一次以聚焦该项目并单击两次以拖动它之后。

如何替换此代码以缩短长点击效果?感谢您的帮助

好的,所以我对DynamicListView做了三个主要更改.java

1)我将setter更改为setOnItemClickListener

 public void init(Context context) {
    setOnItemClickListener(mOnItemClickListener);
       //setOnScrollListener(mScrollListener);
    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    mSmoothScrollAmountAtEdge = (int) (SMOOTH_SCROLL_AMOUNT_AT_EDGE / metrics.density);
        }

2)我使用了onItemClickListener而不是onItemLongClickListener使用捕获块异常

        public  AdapterView.OnItemClickListener mOnItemClickListener = new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
            long id){
           try {
            mTotalOffset = 0;
            //Toast.makeText(getContext(), "OnItemClickListener", 100).show();

            int position = pointToPosition(mDownX, mDownY);
            int itemNum = position - getFirstVisiblePosition();
            View selectedView = getChildAt(itemNum);
            mMobileItemId = getAdapter().getItemId(position);
            mHoverCell = getAndAddHoverView(selectedView);
            selectedView.setVisibility(INVISIBLE);
            mCellIsMobile = true;
            updateNeighborViewsForID(mMobileItemId);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            //e.printStackTrace();

        }
    }
};

3)我更改了下面的代码,以便当您按下时,它将在同一时刻调用键up事件,从而调用onItemClickListener

唯一让我烦恼的是,如果你触摸它以快速触摸它,它就会

停留在那里,但当你再次按下它时它就会消失。

@Override

  public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        //Toast.makeText(getContext(),"previously touched = true",Toast.LENGTH_SHORT).show();
        if (passThroughActionUp){touchEventsEnded();passThroughActionUp = false; }
        passThroughActionDown = true;
        mDownX = (int) event.getX();
        mDownY = (int) event.getY();
        mActivePointerId = event.getPointerId(0);
        event.setAction(MotionEvent.ACTION_UP);
        break;
    case MotionEvent.ACTION_MOVE:
        if (mActivePointerId == INVALID_POINTER_ID) {
            break;
        }
         //Toast.makeText(getContext(),"  passthroughActionMove = true",Toast.LENGTH_SHORT).show();
        int pointerIndex = event.findPointerIndex(mActivePointerId);
        mLastEventY = (int) event.getY(pointerIndex);
        int deltaY = mLastEventY - mDownY;
            //mCellIsMobile being true means that you are in a dragging event.
        if (mCellIsMobile) {
            mHoverCellCurrentBounds.offsetTo(mHoverCellOriginalBounds.left,
                    mHoverCellOriginalBounds.top + deltaY + mTotalOffset);
            mHoverCell.setBounds(mHoverCellCurrentBounds);
            invalidate();
              handleCellSwitch();
            mIsMobileScrolling = false;
              handleMobileCellScroll();
            return false;
        }
        break;
    case MotionEvent.ACTION_UP:
        //if (passThroughActionDown){touchEventsEnded();passThroughActionDown = false; break;}
        passThroughActionUp = true;
        touchEventsEnded();
        break;
    case MotionEvent.ACTION_CANCEL:
        touchEventsCancelled();
        break;
    case MotionEvent.ACTION_POINTER_UP:
        /*
         * If a multitouch event took place and the original touch dictating
         * the movement of the hover cell has ended, then the dragging event
         * ends and the hover cell is animated to its corresponding position
         * in the listview.
         */
        pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
        final int pointerId = event.getPointerId(pointerIndex);
        if (pointerId == mActivePointerId) {
            touchEventsEnded();
        }
        break;
    default:
        break;
    }
    return super.onTouchEvent(event);
}

我找到了解决方案。我为我的列表使用了一个自定义适配器来侦听每个项目,并且我已经使用模式观察器在LongCustomItemClick上创建了一个侦听器。

适配器的 getView 的代码:

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

这里 updateOnItemCustomLongClickListener(); 对应于观察器的更新,它执行覆盖方法 onCustomLongClick(由我的模式观察器的接口生成),该方法替换了主题顶部的 AdapterView.OnItemLongClickListener。时间对于长点击是长点击时间。

最新更新