SlidingUpPanelLayout和ScrollView

use*_*624 5 android slideup android-sliding android-scrollview android-event

我有一个SlidingUpPanelLayout,它将图像保存为顶视图,还有一个需要滑动的视图寻呼机.它viewpager有3个片段,其中两个是列表视图.所以我希望能够在拉起时扩展视图寻呼机,一旦视图寻呼机启动,我希望能够scrollviews在片段内部滚动.但是当拉下来scrollview以防万一没有滚动时,我想开始崩溃viewpager.因此SlidingUpPanelLayout,如果没有更多要滚动的内容,请建议如何在拉动滚动视图时进行折叠?

在这里,我发布了一些代码:我尝试捕获触摸事件并onInterceptTouchEvent以下列方式覆盖SlidingUpPanel 函数:

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (isHandled) {
        Log.i("interceptToch", "HEREEE");
        return onTouchEvent(ev);
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

所以当SlidingUpPanelLayout扩展时,我设置了isHandled = false.因此,当slidingUpPanelLayout展开时,所有触摸事件都将传递给其子视图.

而且我也放入onTouchEventscrollView,以便解锁SlidingUpPanelLayout.onInterceptTouchEvent:

public boolean onTouch(View v, MotionEvent event) {
    int action = event.getAction();
    if (action == MotionEvent.ACTION_DOWN) {
        scroll = 0;
        y = event.getY();
    } else if (action == MotionEvent.ACTION_MOVE) {
        if (scroll_view_summary.getScrollY() == 0 && event.getY() > y) {
            scroll = scroll + 1;
            if (scroll > 2) {
                // the user has pulled the list and the slidingUpPanelLauout 
                // should be able to handle the toch events again
                SlidingUpPanelLayoutCustom las = 
                    ((SaleDetailsActivity) getActivity()).getLayout();
                las.setHandle(true);
                scroll = 0;
                return false;
            }
        }
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用.问题是一旦scrollview.onTouch事件进入MotionEvent.ACTION_MOVE SlidingUpPanelLayout.onInterceptTouchEvent就没有被调用.SlidingUpPanelLayout.onInterceptTouchEvent被称为MotionEvent.ACTION_CANCEL.这意味着事件无法传递给SlidingUpPanelLayout,并且面板无法折叠.

the*_*hal 15

不幸的是,出于onInterceptTouchEvent上述原因,您不能依赖SlidingUpPanelLayout的方法.一旦子视图的onTouchEvent方法返回true,onInterceptTouchEvent就不再调用.

我的解决方案有点复杂,但它可以让你实现你想要的(我认为).单个触摸/拖动事件会将面板拖动到位,一旦就位,继续滚动子视图.同样,当向下拖动时,单个触摸/拖动事件可以滚动子视图,并且一旦完全滚动,将开始向下拖动面板.

更新2015-04-12更新到版本3.0.0的SlidingUpPanelLayout代码.还考虑了ListViews而不仅仅是ScrollViews.

1)res/SlidingUpPanel的库项目文件夹中,打开attrs.xml并添加

<attr name="scrollView" format="reference" />
Run Code Online (Sandbox Code Playgroud)

您将使用它来识别单个子视图,该视图将在面板拖动到位后篡改触摸事件.在布局xml文件中,您可以添加

sothree:scrollView="@+id/myScrollView"
Run Code Online (Sandbox Code Playgroud)

或者无论您的scrollView的ID是什么.还要确保您没有声明sothree:dragViewID,因此整个视图都是可拖动的.

其余的步骤都在SlidingUpPanelLayout.java... 内完成

2) 声明以下变量:

View mScrollView;
int mScrollViewResId = -1;
boolean isChildHandlingTouch = false;
float mPrevMotionX;
float mPrevMotionY;
Run Code Online (Sandbox Code Playgroud)

3)在构造函数中,刚mDragViewResId设置后,添加以下行:

mScrollViewResId = ta.getResourceId(R.styleable.SlidingUpPanelLayout_scrollView, -1);
Run Code Online (Sandbox Code Playgroud)

4)onFinishInflate,添加以下代码:

if (mScrollViewResId != -1) {
    mScrollView = findViewById(mScrollViewResId);
}
Run Code Online (Sandbox Code Playgroud)

5) 添加以下方法:

private boolean isScrollViewUnder(int x, int y) {
    if (mScrollView == null)
        return false;

    int[] viewLocation = new int[2];
    mScrollView.getLocationOnScreen(viewLocation);
    int[] parentLocation = new int[2];
    this.getLocationOnScreen(parentLocation);
    int screenX = parentLocation[0] + x;
    int screenY = parentLocation[1] + y;
    return screenX >= viewLocation[0] && 
           screenX < viewLocation[0] + mScrollView.getWidth() && 
           screenY >= viewLocation[1] && 
           screenY < viewLocation[1] + mScrollView.getHeight();
}
Run Code Online (Sandbox Code Playgroud)

6) 删除onInterceptTouchEvent.

7) 修改onTouchEvent为以下内容:

public boolean onTouchEvent(MotionEvent ev) {
    if (!isEnabled() || !isTouchEnabled()) {
        return super.onTouchEvent(ev);
    }
    try {
        mDragHelper.processTouchEvent(ev);

        final int action = ev.getAction();
        boolean wantTouchEvents = false;

        switch (action & MotionEventCompat.ACTION_MASK) {
            case MotionEvent.ACTION_UP: {
                final float x = ev.getX();
                final float y = ev.getY();
                final float dx = x - mInitialMotionX;
                final float dy = y - mInitialMotionY;
                final int slop = mDragHelper.getTouchSlop();
                View dragView = mDragView != null ? mDragView : mSlideableView;

                if (dx * dx + dy * dy < slop * slop &&
                        isDragViewUnder((int) x, (int) y) &&
                        !isScrollViewUnder((int) x, (int) y)) {
                    dragView.playSoundEffect(SoundEffectConstants.CLICK);

                    if ((PanelState.EXPANDED != mSlideState) && (PanelState.ANCHORED != mSlideState)) {
                        setPanelState(PanelState.ANCHORED);
                    } else {
                        setPanelState(PanelState.COLLAPSED);
                    }
                    break;
                }
                break;
            }
        }

        return wantTouchEvents;
    } catch (Exception ex) {
        ex.printStackTrace();
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

8) 添加以下方法:

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    // Identify if we want to handle the touch event in this class.
    // We do this here because we want to be able to handle the case
    // where a child begins handling a touch event, but then the
    // parent takes over. If we rely on onInterceptTouchEvent, we
    // lose control of the touch as soon as the child handles the event.
    if (mScrollView == null)
        return super.dispatchTouchEvent(ev);

    final int action = MotionEventCompat.getActionMasked(ev);

    final float x = ev.getX();
    final float y = ev.getY();

    if (action == MotionEvent.ACTION_DOWN) {
        // Go ahead and have the drag helper attempt to intercept
        // the touch event. If it won't be dragging, we'll cancel it later.
        mDragHelper.shouldInterceptTouchEvent(ev);

        mInitialMotionX = mPrevMotionX = x;
        mInitialMotionY = mPrevMotionY = y;

        isChildHandlingTouch = false;
    } else if (action == MotionEvent.ACTION_MOVE) {
        float dx = x - mPrevMotionX;
        float dy = y - mPrevMotionY;
        mPrevMotionX = x;
        mPrevMotionY = y;

        // If the scroll view isn't under the touch, pass the
        // event along to the dragView.
        if (!isScrollViewUnder((int) x, (int) y))
            return this.onTouchEvent(ev);

        // Which direction (up or down) is the drag moving?
        if (dy > 0) { // DOWN
            // Is the child less than fully scrolled?
            // Then let the child handle it.
            if (isScrollViewScrolling()) {
                isChildHandlingTouch = true;
                return super.dispatchTouchEvent(ev);
            }

            // Was the child handling the touch previously?
            // Then we need to rejigger things so that the
            // drag panel gets a proper down event.
            if (isChildHandlingTouch) {
                // Send an 'UP' event to the child.
                MotionEvent up = MotionEvent.obtain(ev);
                up.setAction(MotionEvent.ACTION_UP);
                super.dispatchTouchEvent(up);
                up.recycle();

                // Send a 'DOWN' event to the panel. (We'll cheat
                // and hijack this one)
                ev.setAction(MotionEvent.ACTION_DOWN);
            }

            isChildHandlingTouch = false;
            return this.onTouchEvent(ev);
        } else if (dy < 0) { // UP
            // Is the panel less than fully expanded?
            // Then we'll handle the drag here.
            if (mSlideOffset < 1.0f) {
                isChildHandlingTouch = false;
                return this.onTouchEvent(ev);
            }

            // Was the panel handling the touch previously?
            // Then we need to rejigger things so that the
            // child gets a proper down event.
            if (!isChildHandlingTouch) {
                mDragHelper.cancel();
                ev.setAction(MotionEvent.ACTION_DOWN);
            }

            isChildHandlingTouch = true;
            return super.dispatchTouchEvent(ev);
        }
    } else if ((action == MotionEvent.ACTION_CANCEL) ||
            (action == MotionEvent.ACTION_UP)) {
        if (!isChildHandlingTouch) {
            final float dx = x - mInitialMotionX;
            final float dy = y - mInitialMotionY;
            final int slop = mDragHelper.getTouchSlop();

            if ((mIsUsingDragViewTouchEvents) && (dx * dx + dy * dy < slop * slop))
                return super.dispatchTouchEvent(ev);

            return this.onTouchEvent(ev);
        }
    }

    // In all other cases, just let the default behavior take over.
    return super.dispatchTouchEvent(ev);
}
Run Code Online (Sandbox Code Playgroud)

9)添加以下方法以确定scrollView是否仍在滚动.处理ScrollView和ListView的案例:

/**
 * Computes the scroll position of the the scrollView, if set.
 * @return
 */
private boolean isScrollViewScrolling() {
    if (mScrollView == null)
        return false;

    // ScrollViews are scrolling when getScrollY() is a value greater than 0.
    if (mScrollView instanceof ScrollView) {
        return (mScrollView.getScrollY() > 0);
    }
    // ListViews are scrolling if the first child is not displayed, or if the first child has an offset > 0
    else if (mScrollView instanceof ListView) {
        ListView lv = (ListView) mScrollView;

        if (lv.getFirstVisiblePosition() > 0)
            return true;

        View v = lv.getChildAt(0);
        int top = (v == null) ? (0) : (-v.getTop() + lv.getFirstVisiblePosition() * lv.getHeight());
        return top > 0;
    }

    return false;
}
Run Code Online (Sandbox Code Playgroud)

10)(可选)添加以下方法以允许您在运行时设置scrollView(即,您希望在面板中放置一个片段,并且片段的子节点具有您要滚动的ScrollView/ListView):

public void setScrollView(View scrollView) {
    mScrollView = scrollView;
}
Run Code Online (Sandbox Code Playgroud)

我们现在完全在这个课程中管理触摸事件的处理.如果我们向上拖动面板并将其完全滑动到位,我们取消拖动,然后在mScrollView孩子中欺骗新的触摸.如果我们滚动孩子并到达顶部,我们会欺骗孩子的"up"事件,并为拖拽提供新的触摸.这也允许在其他子窗口小部件上点击事件.

已知问题 我们欺骗的"向上"/"向下"事件可能会无意中触发scrollView的子元素上的click事件.

  • 我应该注意,这个解决方案假定没有设置任何显式的dragView,因此整个面板是可拖动的. (3认同)

Mik*_*nov 6

我有同样的问题但在我的应用程序中有ListView而不是ScrollView.我无法应用themarshal的答案为我的问题工作.但我找到了基于themarshal,Chris的答案和Maria Sakharova的评论的解决方案

首先我找不到变量mCanSlide和mIsSlidingEnabled以及方法expandPane(mAnchorPoint)和collapsePane()所以我使用下一个代码:

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (!isEnabled() || !isTouchEnabled()) {
        return super.onTouchEvent(ev);
    }
    try {
        mDragHelper.processTouchEvent(ev);

        final int action = ev.getAction();
        boolean wantTouchEvents = false;

        switch (action & MotionEventCompat.ACTION_MASK) {
            case MotionEvent.ACTION_UP: {
                final float x = ev.getX();
                final float y = ev.getY();
                final float dx = x - mInitialMotionX;
                final float dy = y - mInitialMotionY;
                final int slop = mDragHelper.getTouchSlop();
                View dragView = mDragView != null ? mDragView : mSlideableView;

                if (dx * dx + dy * dy < slop * slop &&
                        isDragViewUnder((int) x, (int) y) &&
                        !isScrollViewUnder((int) x, (int) y)) {
                    dragView.playSoundEffect(SoundEffectConstants.CLICK);
                    if (!isExpanded() && !isAnchored()) {
                        //expandPane(mAnchorPoint);
                        setPanelState(PanelState.ANCHORED);
                    } else {
                        //collapsePane();
                        setPanelState(PanelState.COLLAPSED);
                    }
                    break;
                }
                break;
            }
        }

        return wantTouchEvents;
    } catch (Exception ex){
        ex.printStackTrace();
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

需要try/catch,因为当使用两根手指时异常加注.

第二个克里斯的答案是必须履行的.

然后由于ListView的方法,getScrollY()总是返回零,我在方法dispatchTouchEvent(MotionEvent ev)中略微改变代码:

这个:

if (mScrollView.getScrollY() > 0) {
   isChildHandlingTouch = true;
   return super.dispatchTouchEvent(ev);
}
Run Code Online (Sandbox Code Playgroud)

至:

if (((ListView)mScrollView).getFirstVisiblePosition() > 0 ||             getFirstChildTopOffset((ListView) mScrollView) > 0){
   isChildHandlingTouch = true;
   return super.dispatchTouchEvent(ev);
} 

//at some other place in class SlidingUpPanelLayout 
public int getFirstChildTopOffset(ListView list){
    View v = list.getChildAt(0);
    int top = (v == null) ? 0 : (list.getPaddingTop() - v.getTop());
    return top;
}
Run Code Online (Sandbox Code Playgroud)

此外,我的应用程序将谷歌地图作为主要内容,它也必须得到MotionEvent,因为玛丽亚萨哈罗娃说我们必须返回this.onTouchEvent(ev)|| super.dispatchTouchEvent(ev)而不是this.onTouchEvent(ev)在两个地方.我们必须更改此代码:

if (!isScrollViewUnder((int) x, (int) y))
   return this.onTouchEvent(ev);
Run Code Online (Sandbox Code Playgroud)

至:

if (!isScrollViewUnder((int) x, (int) y))
   return this.onTouchEvent(ev) || super.dispatchTouchEvent(ev);
Run Code Online (Sandbox Code Playgroud)

在这种情况下,如果主要内容必须获取MotionEvent,则需要super.dispatchTouchEvent(ev).

第二个代码:

} else if ((action == MotionEvent.ACTION_CANCEL) ||
            (action == MotionEvent.ACTION_UP)) {
    if (!isChildHandlingTouch) {
        final float dx = x - mInitialMotionX;
        final float dy = y - mInitialMotionY;
        final int slop = mDragHelper.getTouchSlop();

        if ((mIsUsingDragViewTouchEvents) &&
                    (dx * dx + dy * dy < slop * slop))
            return super.dispatchTouchEvent(ev);

        return this.onTouchEvent(ev);
    }
}
Run Code Online (Sandbox Code Playgroud)

至:

} else if ((action == MotionEvent.ACTION_CANCEL) ||
            (action == MotionEvent.ACTION_UP)) {
   if (!isChildHandlingTouch) {
        final float dx = x - mInitialMotionX;
        final float dy = y - mInitialMotionY;
        final int slop = mDragHelper.getTouchSlop();

        if ((mIsUsingDragViewTouchEvents) &&
                    (dx * dx + dy * dy < slop * slop))
            return super.dispatchTouchEvent(ev);

        return this.onTouchEvent(ev) || super.dispatchTouchEvent(ev);
    }
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,需要super.dispatchTouchEvent(ev)来扩展面板.

总结方法dispatchTouchEvent(MotionEvent ev)将是下一个:

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    // Identify if we want to handle the touch event in this class.
    // We do this here because we want to be able to handle the case
    // where a child begins handling a touch event, but then the
    // parent takes over. If we rely on onInterceptTouchEvent, we
    // lose control of the touch as soon as the child handles the event.
    if (mScrollView == null)
        return super.dispatchTouchEvent(ev);

    final int action = MotionEventCompat.getActionMasked(ev);

    final float x = ev.getX();
    final float y = ev.getY();

    if (action == MotionEvent.ACTION_DOWN) {
        // Go ahead and have the drag helper attempt to intercept
        // the touch event. If it won't be dragging, we'll cancel it later.
        mDragHelper.shouldInterceptTouchEvent(ev);

        mInitialMotionX = mPrevMotionX = x;
        mInitialMotionY = mPrevMotionY = y;

        isChildHandlingTouch = false;
    } else if (action == MotionEvent.ACTION_MOVE) {
        float dx = x - mPrevMotionX;
        float dy = y - mPrevMotionY;
        mPrevMotionX = x;
        mPrevMotionY = y;

        // If the scroll view isn't under the touch, pass the
        // event along to the dragView.
        if (!isScrollViewUnder((int) x, (int) y))
            //return this.onTouchEvent(ev);
            return this.onTouchEvent(ev) || super.dispatchTouchEvent(ev);

        // Which direction (up or down) is the drag moving?
        if (dy > 0) { // DOWN
            // Is the child less than fully scrolled?
            // Then let the child handle it.
            //if (mScrollView.getScrollY() > 0) {
            if (((ListView)mScrollView).getFirstVisiblePosition() > 0 || getFirstChildTopOffset((ListView) mScrollView) > 0){
                isChildHandlingTouch = true;
                return super.dispatchTouchEvent(ev);
            }

            // Was the child handling the touch previously?
            // Then we need to rejigger things so that the
            // drag panel gets a proper down event.
            if (isChildHandlingTouch) {
                // Send an 'UP' event to the child.
                MotionEvent up = MotionEvent.obtain(ev);
                up.setAction(MotionEvent.ACTION_UP);
                super.dispatchTouchEvent(up);
                up.recycle();

                // Send a 'DOWN' event to the panel. (We'll cheat
                // and hijack this one)
                ev.setAction(MotionEvent.ACTION_DOWN);
            }

            isChildHandlingTouch = false;
            return this.onTouchEvent(ev);
        } else if (dy < 0) { // UP
            // Is the panel less than fully expanded?
            // Then we'll handle the drag here.
            //if (mSlideOffset > 0.0f) {
            if (mSlideOffset < 1.0f) {
                isChildHandlingTouch = false;
                return this.onTouchEvent(ev);
                //return this.onTouchEvent(ev) || super.dispatchTouchEvent(ev);
            }

            // Was the panel handling the touch previously?
            // Then we need to rejigger things so that the
            // child gets a proper down event.
            if (!isChildHandlingTouch) {
                mDragHelper.cancel();
                ev.setAction(MotionEvent.ACTION_DOWN);
            }

            isChildHandlingTouch = true;
            return super.dispatchTouchEvent(ev);
        }
    } else if ((action == MotionEvent.ACTION_CANCEL) ||
            (action == MotionEvent.ACTION_UP)) {
        if (!isChildHandlingTouch) {
            final float dx = x - mInitialMotionX;
            final float dy = y - mInitialMotionY;
            final int slop = mDragHelper.getTouchSlop();

            if ((mIsUsingDragViewTouchEvents) &&
                    (dx * dx + dy * dy < slop * slop))
                return super.dispatchTouchEvent(ev);

            //return this.onTouchEvent(ev);
            return this.onTouchEvent(ev) || super.dispatchTouchEvent(ev);
        }
    }

    // In all other cases, just let the default behavior take over.
    return super.dispatchTouchEvent(ev);
}
Run Code Online (Sandbox Code Playgroud)