touch listener优先于listscroll侦听器

时间:2013-07-22 00:57:22

标签: android listview swipe horizontallist

我有一个滑动消除工作的水平列表,除了滑动消除优先于滚动,即如果我从左向右滑动,但在最轻微的角度,它会认为我试图轻扫解除一个项目而不是滚动列表沿。 我认为最简单的方法是让它反过来但不知道如何改变它或者是否有更好的方法

我修改了Lucas Rocha的twowayview和Roman Nurik / Tim Roes swipedismissundolist

我不确定所有这些代码是否应该是我应该查看的地方,但我可以说它是相关的。

mainactivity

   listView = (TwoWayView) findViewById(R.id.listView1);


    //need to add in emptyview and list view to show and gone them when emtpy/full


   imageAdapter = new ImageAdapter(this, products,emptyview,listView);


    listView.setAdapter(imageAdapter);


    VSwipeDismissList.OnDismissCallback callback = new VSwipeDismissList.OnDismissCallback() {
        @Override
        public VSwipeDismissList.Undoable onDismiss(AbsListView listView, final int position) {
            // Delete the item from your adapter (sample code):

            imageAdapter.remove(position);
            return null;

            }

    };

swipedismiss

/**
 * Returns an {@link android.widget.AbsListView.OnScrollListener} to be
 * added to the {@link ListView} using
 * {@link ListView#setOnScrollListener(android.widget.AbsListView.OnScrollListener)}.
 * If a scroll listener is already assigned, the caller should still pass
 * scroll changes through to this listener. This will ensure that this
 * {@link SwipeDismissListViewTouchListener} is paused during list view
 * scrolling.</p>
 *
 * @see {@link SwipeDismissListViewTouchListener}
 */
private AbsListView.OnScrollListener makeScrollListener() {
    return new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView absListView, int scrollState) {
            setEnabled(scrollState != AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
        }

        @Override
        public void onScroll(AbsListView absListView, int i, int i1, int i2) {
        }
    };
}

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {...

twowayview

    private boolean maybeStartScrolling(int delta) {
    final boolean isOverScroll = (mOverScroll != 0);
    if (Math.abs(delta) <= mTouchSlop && !isOverScroll) {
        return false;
    }

    if (isOverScroll) {
        mTouchMode = TOUCH_MODE_OVERSCROLL;
    } else {
        mTouchMode = TOUCH_MODE_DRAGGING;
    }

    // Time to start stealing events! Once we've stolen them, don't
    // let anyone steal from us.
    final ViewParent parent = getParent();
    if (parent != null) {
        parent.requestDisallowInterceptTouchEvent(true);
    }

    cancelCheckForLongPress();

    setPressed(false);
    View motionView = getChildAt(mMotionPosition - mFirstPosition);
    if (motionView != null) {
        motionView.setPressed(false);
    }

    reportScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);

    return true;
}

private void maybeScroll(int delta) {
    if (mTouchMode == TOUCH_MODE_DRAGGING) {
        handleDragChange(delta);
    } else if (mTouchMode == TOUCH_MODE_OVERSCROLL) {
        handleOverScrollChange(delta);
    }
}

private void handleDragChange(int delta) {
    // Time to start stealing events! Once we've stolen them, don't
    // let anyone steal from us.
    final ViewParent parent = getParent();
    if (parent != null) {
        parent.requestDisallowInterceptTouchEvent(true);
    }

    final int motionIndex;
    if (mMotionPosition >= 0) {
        motionIndex = mMotionPosition - mFirstPosition;
    } else {
        // If we don't have a motion position that we can reliably track,
        // pick something in the middle to make a best guess at things below.
        motionIndex = getChildCount() / 2;
    }

    int motionViewPrevStart = 0;
    View motionView = this.getChildAt(motionIndex);
    if (motionView != null) {
        motionViewPrevStart = (mIsVertical ? motionView.getTop() : motionView.getLeft());
    }

    boolean atEdge = scrollListItemsBy(delta);

    motionView = this.getChildAt(motionIndex);
    if (motionView != null) {
        final int motionViewRealStart =
                (mIsVertical ? motionView.getTop() : motionView.getLeft());

        if (atEdge) {
            final int overscroll = -delta - (motionViewRealStart - motionViewPrevStart);
            updateOverScrollState(delta, overscroll);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我发现了一个非常重复的问题here

我将操作向下添加并将动作移动代码添加到swipedismiss的onTouch方法

     @Override
   public boolean onInterceptTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {
    case MotionEvent.ACTION_DOWN:
        xDistance = yDistance = 0f;
        lastX = ev.getX();
        lastY = ev.getY();
        break;
    case MotionEvent.ACTION_MOVE:
        final float curX = ev.getX();
        final float curY = ev.getY();
        xDistance += Math.abs(curX - lastX);
        yDistance += Math.abs(curY - lastY);
        lastX = curX;
        lastY = curY;
        if(xDistance > yDistance)
            return false;
}