开发者

TextView Within Gallery

开发者 https://www.devze.com 2023-03-20 09:17 出处:网络
i have scrollable TextView and Gallery when i enable the onTouchEvent() for the TextView, i cant navigate throught the 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开发者_JS百科 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?


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


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;
}


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


insted of onTouch event why don't you use onclick event for textview inside gallery onMotionEvent


try to set return false in in onTouch event.


it is possible that TextView and/or Gallery are taking the focus away from the ListView if you are using one or the scrolling ability thus making it not able to scroll up and down, try removing focusable for the TextView and Gallery and see if that helps at all. Then you should be able to still use your touchEvents... just a thought, not sure if it will actually fix the issue though. wish I could be more help.


Create a custom view that extends a container layout and contains your scrollable TextView. Use this as your gallery items.

0

精彩评论

暂无评论...
验证码 换一张
取 消