【问题标题】:Android: How do I place a ScrollView (with text in) into Gallery?Android:如何将 ScrollView(包含文本)放入 Gallery?
【发布时间】:2011-02-07 06:16:28
【问题描述】:

我将在 android 应用程序的图库中放置文本。文本位于滚动视图中。一切正常,但在向右或向左拖动文本时,没有显示下一页/元素(屏幕保持静止)。如果我在滚动视图之外有一些东西,它会在拖动时更改为下一个元素。

谁能帮忙?

【问题讨论】:

    标签: android android-layout android-widget scrollview android-gallery


    【解决方案1】:

    您需要覆盖onInterceptTouchEvent - 您可以使用它在MotionEvents 传递到ScrollView 之前获取它们,然后将它们重定向到您的ViewGroup(在这种情况下为Gallery)如果你愿意。

    如果用户将手指向左或向右移动得太远,以下类会将MotionEvents 重定向到您的Gallery。此外,如果用户向上或向下移动手指相当多,那么向左或向右移动手指将不再有效果,因此您不必担心在进行大量滚动时图库会发生变化。

    class ScrollViewGallery extends Gallery {
    
        /** 
         * The distance the user has to move their finger, in density independent
         * pixels, before we count the motion as A) intended for the ScrollView if
         * the motion is in the vertical direction or B) intended for ourselfs, if
         * the motion is in the horizontal direction - after the user has moved this
         * amount they are "locked" into this direction until the next ACTION_DOWN
         * event
         */
        private static final int DRAG_BOUNDS_IN_DP = 20;
    
        /**
         * A value representing the "unlocked" state - we test all MotionEvents
         * when in this state to see whether a lock should be make
         */
        private static final int SCROLL_LOCK_NONE = 0;
    
        /**
         * A value representing a lock in the vertical direction - once in this state
         * we will never redirect MotionEvents from the ScrollView to ourself
         */
        private static final int SCROLL_LOCK_VERTICAL = 1;
    
        /**
         * A value representing a lock in the horizontal direction - once in this
         * state we will not deliver any more MotionEvents to the ScrollView, and
         * will deliver them to ourselves instead.
         */
        private static final int SCROLL_LOCK_HORIZONTAL = 2;
    
        /**
         * The drag bounds in density independent pixels converted to actual pixels
         */
        private int mDragBoundsInPx = 0;
    
        /**
         * The coordinates of the intercepted ACTION_DOWN event
         */
        private float mTouchStartX;
        private float mTouchStartY;
    
        /**
         * The current scroll lock state
         */
        private int mScrollLock = SCROLL_LOCK_NONE;
    
        public ScrollViewGallery(Context context) {
            super(context);
            initCustomGallery(context);
        }
    
        public ScrollViewGallery(Context context, AttributeSet attrs) {
            super(context, attrs);
            initCustomGallery(context);
        }
    
        public ScrollViewGallery(Context context, AttributeSet attrs,
                int defStyle) {
            super(context, attrs, defStyle);
            initCustomGallery(context);
        }
    
        private void initCustomGallery(Context context) {
            final float scale = context.getResources().getDisplayMetrics().density;
            mDragBoundsInPx = (int) (scale*DRAG_BOUNDS_IN_DP + 0.5f);
        }
    
        /**
         * This will be called before the intercepted views onTouchEvent is called
         * Return false to keep intercepting and passing the event on to the target view
         * Return true and the target view will recieve ACTION_CANCEL, and the rest of the
         * events will be delivered to our onTouchEvent
         */
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            final int action = ev.getAction();
            switch (action) {
            case MotionEvent.ACTION_DOWN:
                mTouchStartX = ev.getX();
                mTouchStartY = ev.getY();
                mScrollLock = SCROLL_LOCK_NONE;
    
                /**
                 * Deliver the down event to the Gallery to avoid jerky scrolling
                 * if we decide to redirect the ScrollView events to ourself
                 */
                super.onTouchEvent(ev);
                break;
    
            case MotionEvent.ACTION_MOVE:
                if (mScrollLock == SCROLL_LOCK_VERTICAL) {
                    // keep returning false to pass the events
                    // onto the ScrollView
                    return false;
                }
    
                final float touchDistanceX = (ev.getX() - mTouchStartX);
                final float touchDistanceY = (ev.getY() - mTouchStartY);
    
                if (Math.abs(touchDistanceY) > mDragBoundsInPx) {
                    mScrollLock = SCROLL_LOCK_VERTICAL;
                    return false;
                }
                if (Math.abs(touchDistanceX) > mDragBoundsInPx) {
                    mScrollLock = SCROLL_LOCK_HORIZONTAL; // gallery action
                    return true; // redirect MotionEvents to ourself
                }
                break;
    
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                // if we're still intercepting at this stage, make sure the gallery
                // also recieves the up/cancel event as we gave it the down event earlier
                super.onTouchEvent(ev);
                break;
            }
    
            return false;
        }
    }
    

    【讨论】:

      【解决方案2】:

      我明白你的问题。在覆盖方法之后,这种情况已经由我处理。我不确定这将是最好的解决方案,可能有人有比这个更有效的解决方案,但它对我有用。

      我使用了自定义的画廊对象和覆盖的方法。

       public boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {
          return e2.getX() > e1.getX();
       }
      
       @Override
       public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
         float velocityY) {
          return super.onFling(e1, e2, 0, velocityY);
       }
      

      【讨论】:

        【解决方案3】:

        我发现如果您将文本视图置于垂直滚动条模式,它只会向上或向下移动。如果我们想水平滑动,你必须触摸到文本视图之外。

        【讨论】:

        • 如果您为 Gallery 覆盖 onInterceptTouchEvent,那么您可以拦截传递到 ScrollView 的触摸并将它们重定向到 Gallery(以便它向左/向右拖动),如果返回 true用户将手指向左或向右移动得太远。
        猜你喜欢
        • 2012-10-03
        • 2011-10-01
        • 2011-03-11
        • 2017-11-13
        • 2017-10-20
        • 2018-12-08
        • 2015-08-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多