Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextView Within Gallery

i have scrollable TextView and Gallery when i enable the onTouchEvent() for the TextView, i cant navigate throught the Gallery and if i disable the onTouchEvent(), i can scroll right and left throught the Gallery but can't scroll up down into TextView.

i've tried to do tricks, like send the same MotionEvent() to both Gallery And TextView .. any ideas how to solve it?

like image 383
Mohammad Ersan Avatar asked Jul 14 '11 06:07

Mohammad Ersan


3 Answers

Have you tried using ScrollView in gallery? Here is some working example: ScrollView inside Gallery, both scrolling independently

like image 156
peter.bartos Avatar answered Sep 17 '22 15:09

peter.bartos


Hey you have to try this code it will help you in your problem

float oldx;
float oldy;

@Override
public boolean onTouchEvent(MotionEvent event) {
    boolean result = false;
    if(event.getAction() == MotionEvent.ACTION_DOWN) {
        result = super.onTouchEvent(event);
    } else if (event.getAction() == MotionEvent.ACTION_UP) {   
        result = super.onTouchEvent(event);
    } else  if (event.getAction() == MotionEvent.ACTION_MOVE) {
        /**
         * Condition :: Math.abs(oldx - event.getX()) > 10 will check weather user had drag horizontal or vertical. 
         * You can change value 10 according to your requirement 
         */
        if(Math.abs(oldx - event.getX()) > 10) {
            result = super.onTouchEvent(event);
        } else {
            result =  true;
        }
    }   
    oldx = event.getX();
    oldy = event.getY();
    return result;
}
like image 29
Dharmendra Avatar answered Sep 18 '22 15:09

Dharmendra


my answer :

public class CustomGallery extends Gallery {

    public CustomGallery(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

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

    public CustomGallery(Context context) {
        super(context);
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        float r = (velocityX > 0 ? Math.min(800f, velocityX) : Math.max(-800f, velocityX));
        return super.onFling(e1, e2, r, velocityY);
    }

    float oldX, oldY;
    boolean isDown = false;

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                oldX = ev.getX();
                oldY = ev.getY();
                isDown = true;
                return super.onInterceptTouchEvent(ev);

            case MotionEvent.ACTION_MOVE:
                if (isDown) {
                    float diffX = Math.abs(ev.getX() - oldX);
                    float diffY = Math.abs(ev.getY() - oldY) + 20f;
                    if (diffY > diffX) {
                        return false;// handled by TextView
                    }
                }
                return true;// handled by Gallery

            case MotionEvent.ACTION_UP:
                isDown = false;
                return super.onInterceptTouchEvent(ev);

            default:
                return super.onInterceptTouchEvent(ev);
        }
    }

}

thanks for @teepee for providing a huge hint for solution :D

like image 23
Mohammad Ersan Avatar answered Sep 19 '22 15:09

Mohammad Ersan