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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With