Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onInterceptTouchEvent only gets ACTION_DOWN

Tags:

java

android

Why do ViewGroup's only get ACTION_DOWN in the onInterceptTouchEvent? According to the docs, as long as false is returned it should receive all the event types. http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent%28android.view.MotionEvent%29 Point #3.

Sample code:

public class MainActivity extends Activity {      private static final String TAG = MainActivity.class.getSimpleName();      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(new Container(this));     }      private class Container extends LinearLayout {          public Container(Context context) {             super(context);             setBackgroundColor(0xFF0000FF);         }          @Override         public boolean onInterceptTouchEvent(MotionEvent ev) {             Log.i(TAG, "onInterceptTouchEvent");             int action = ev.getActionMasked();             switch (action) {             case MotionEvent.ACTION_DOWN:                 Log.i(TAG, "onInterceptTouchEvent.ACTION_DOWN");                 break;             case MotionEvent.ACTION_MOVE:                 Log.i(TAG, "onInterceptTouchEvent.ACTION_MOVE");                 break;             case MotionEvent.ACTION_CANCEL:             case MotionEvent.ACTION_UP:                 Log.i(TAG, "onInterceptTouchEvent.ACTION_UP");                 break;             }             return super.onInterceptTouchEvent(ev);         }     } } 
like image 246
user123321 Avatar asked Nov 08 '12 06:11

user123321


1 Answers

I'll answer my own question: onInterceptTouchEvent only get called if the parent has a child view which returns "true" from onTouchEvent. Once the child returns true, the parent now has a chance to intercept that event.

enter image description here

like image 185
user123321 Avatar answered Sep 18 '22 02:09

user123321