I have a custom gallery view for horizontal scrolling and several child views that need to be clicked.
The setting childview.setOnClickListener()does not work because it always consumes a touch event.
Therefore, I used childview.setOnTouchListener()and let its onTouch method return false so that the gallery becomes scrollable.
Everything is fine.
The problem is that the onTouch method for childView fires the ACTION_DOWN event . It does not pass MotionEvent ACTION_UP unless I make the View clickable by setting childview.setClickable(). However, setting the Clickable view itself seems to be consuming the onTouch event to make the gallery view insecure.
Looks like I'm going around here. I would be grateful for any help.
Here is my code
Gallery view:
public class myGallery extends Gallery {
public myGallery(Context ctx, AttributeSet attrSet) {
super(ctx, attrSet);
}
private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){
return e2.getX() > e1.getX();
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){
int kEvent;
if(isScrollingLeft(e1, e2)){
kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
}else{
kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
}
onKeyDown(kEvent, null);
return true;
}
}
in my activity:
gallery.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
childView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
}
return false;
}
});
}
public void onNothingSelected(AdapterView<?> arg0) {}
});
}
source
share