Skip to content Skip to sidebar Skip to footer

Android: How Do I Place A Scrollview (with Text In) Into Gallery?

I am going to place text in gallery in android app. The text is in a scrollView. Everything works fine but on draging the text towards right or left, no next page/ element shows (T

Solution 1:

You need to override onInterceptTouchEvent - you can use this to get the MotionEvents before they are delivered to the ScrollView, and then redirect them to your ViewGroup (Gallery in this case) if you wish.

The following class redirects MotionEvents to your Gallery if the user moves their finger too far left or right. Also if the user moves their finger up or down quite a bit then moving their finger left or right will have no longer have an effect, so you don't have to worry about the Gallery changing while doing a lot of scrolling.

classScrollViewGallery 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
     */privatestaticfinalint 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
     */privatestaticfinalint 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
     */privatestaticfinalint 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.
     */privatestaticfinalint SCROLL_LOCK_HORIZONTAL = 2;

    /**
     * The drag bounds in density independent pixels converted to actual pixels
     */privateint mDragBoundsInPx = 0;

    /**
     * The coordinates of the intercepted ACTION_DOWN event
     */privatefloat mTouchStartX;
    privatefloat mTouchStartY;

    /**
     * The current scroll lock state
     */privateint mScrollLock = SCROLL_LOCK_NONE;

    publicScrollViewGallery(Context context){
        super(context);
        initCustomGallery(context);
    }

    publicScrollViewGallery(Context context, AttributeSet attrs){
        super(context, attrs);
        initCustomGallery(context);
    }

    publicScrollViewGallery(Context context, AttributeSet attrs,
            int defStyle){
        super(context, attrs, defStyle);
        initCustomGallery(context);
    }

    privatevoidinitCustomGallery(Context context){
        finalfloat 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){
        finalint 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 ScrollViewreturnfalse;
            }

            finalfloat touchDistanceX = (ev.getX() - mTouchStartX);
            finalfloat touchDistanceY = (ev.getY() - mTouchStartY);

            if (Math.abs(touchDistanceY) > mDragBoundsInPx) {
                mScrollLock = SCROLL_LOCK_VERTICAL;
                returnfalse;
            }
            if (Math.abs(touchDistanceX) > mDragBoundsInPx) {
                mScrollLock = SCROLL_LOCK_HORIZONTAL; // gallery actionreturntrue; // 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;
        }

        returnfalse;
    }
}

Solution 2:

I understand your question. This situation is already handle by me after overriding method. I am not sure this will be the best solution, may be some one have more efficient solution then this, But it works for me.

I used customised object of gallery with the over ridden method.

publicbooleanisScrollingLeft(MotionEvent e1, MotionEvent e2) {
    return e2.getX() > e1.getX();
 }

 @OverridepublicbooleanonFling(MotionEvent e1, MotionEvent e2, float velocityX,
   float velocityY) {
    returnsuper.onFling(e1, e2, 0, velocityY);
 }

Solution 3:

I found that if you place a textview in a vertical scrollbar mode it only moves up or down. If we want to swipe horizontally, you have to touch outside of the textview.

Post a Comment for "Android: How Do I Place A Scrollview (with Text In) Into Gallery?"