繁体   English   中英

当我使用SlidingMenu / ViewPager和ListView时如何确定TouchEvent?

[英]How to determine the TouchEvent when i use SlidingMenu/ViewPager and ListView?

我在我的应用程序中使用了slideMenu(来自jfeinstein10),我创建了一个从SlidingFragmentActivity扩展的新Activity,在Activity中,我使用来自support.v4的ViewPager来显示一些片段,使用gtRfnkN在ViewPager内的问题ViewPager中回答的方式

public class GalleryViewPager extends ViewPager {

/** the last x position */
private float   lastX;

/** if the first swipe was from left to right (->), dont listen to swipes from the right */
private boolean slidingLeft;

/** if the first swipe was from right to left (<-), dont listen to swipes from the left */
private boolean slidingRight;

public GalleryViewPager(final Context context, final AttributeSet attrs) {
    super(context, attrs);
}

public GalleryViewPager(final Context context) {
    super(context);
}

@Override
public boolean onTouchEvent(final MotionEvent ev) {
    final int action = ev.getAction();
    switch (action) {
        case MotionEvent.ACTION_DOWN:

            // Disallow parent ViewPager to intercept touch events.
            this.getParent().requestDisallowInterceptTouchEvent(true);

            // save the current x position
            this.lastX = ev.getX();

            break;

        case MotionEvent.ACTION_UP:
            // Allow parent ViewPager to intercept touch events.
            this.getParent().requestDisallowInterceptTouchEvent(false);

            // save the current x position
            this.lastX = ev.getX();

            // reset swipe actions
            this.slidingLeft = false;
            this.slidingRight = false;

            break;

        case MotionEvent.ACTION_MOVE:
            /*
             * if this is the first item, scrolling from left to
             * right should navigate in the surrounding ViewPager
             */
            if (this.getCurrentItem() == 0) {
                // swiping from left to right (->)?
                if (this.lastX <= ev.getX() && !this.slidingRight) {
                    // make the parent touch interception active -> parent pager can swipe
                    this.getParent().requestDisallowInterceptTouchEvent(false);
                } else {
                    /*
                     * if the first swipe was from right to left, dont listen to swipes
                     * from left to right. this fixes glitches where the user first swipes
                     * right, then left and the scrolling state gets reset
                     */
                    this.slidingRight = true;

                    // save the current x position
                    this.lastX = ev.getX();
                    this.getParent().requestDisallowInterceptTouchEvent(true);
                }
            } else
            /*
             * if this is the last item, scrolling from right to
             * left should navigate in the surrounding ViewPager
             */
            if (this.getCurrentItem() == this.getAdapter().getCount() - 1) {
                // swiping from right to left (<-)?
                if (this.lastX >= ev.getX() && !this.slidingLeft) {
                    // make the parent touch interception active -> parent pager can swipe
                    this.getParent().requestDisallowInterceptTouchEvent(false);
                } else {
                    /*
                     * if the first swipe was from left to right, dont listen to swipes
                     * from right to left. this fixes glitches where the user first swipes
                     * left, then right and the scrolling state gets reset
                     */
                    this.slidingLeft = true;

                    // save the current x position
                    this.lastX = ev.getX();
                    this.getParent().requestDisallowInterceptTouchEvent(true);
                }
            }

            break;
    }

    super.onTouchEvent(ev);
    return true;
}

 }

但是当我将ListView放在ViewPager的Fragment中时,当我水平移动ListView时,ViewPager保持不动,滑动菜单滑出。您能告诉我如何解决这个问题吗? 非常感谢。

试试这个演示与列表视图,地图滑...希望这将有助于U: https://github.com/jfeinstein10/SlidingMenu

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM