When do we need it ?
Handling touch events in a ViewGroup (e.g., FrameLayout) is tricky because sometimes we need to handle the ViewGroup’s touch event differently with it’s children’s touch event. The challenge is how to make each view receive the event intended for it.
An example would be: For an exensible sprouts view, it needs to be draggable as a ViewGroup itself, but each sprout item inside the extensible sprouts view needs also to be able to receive individual click event. How to make sure each child and their parent receive the right event? We will need to override the onInterceptTouchEvent() method of the ViewGroup.
Intercept Touch Event in a ViewGroup
onInterceptTouchEvent() is called whenever a touch event is detected on the surface of a ViewGroup including its children.
If onInterceptTouchEvent() returns true, it means that the event will be passed to the ViewGroup’s onTouchEvent to be handled and will not be passed to any of the child view. If the onInterceptTouchEvent() return false, it means the event will be passed to the children to handle, but it will give parent view a chance to spy on these events before they are passed to children.
ViewConfiguration
ViewConfiguration is used to access common distances, speeds, and time used by Android System.
Touch Slop: The distance in pixels when a user’s touch can wander before it can be interpreted as a sroll.
Max Fling Velocity: The max velocity to initiate a fling (quick swipe gesture used in Android).
Min Fling Velocity: The min velocity to initiate a fling.
Extend a child view’s Touchable area
Android allows to extend or shrink the touch area of a specific children.
First, get the the hit rectangle of the child in parent’s coordinator
Rect delegateArea = new Rect();
mButton.getHitRect(delegateArea);
Then, change the Rect area
delegateArea.right += 100;
delegateArea.left += 100;
Then, initiate a touch delegate with the revised touch area and the child.
TouchDelegate touchDelegate = new TouchDelegate(delegateArea, mButton);
Last, set the touch delegate on the parent view.
if (View.class.isInstance(mButton.getParent())) {
((View) mButton.getParent()).setTouchDelegate(touchDelegate);
}
明天开始去gym,努力工作,努力运动!好好过好回国前的三周 🙂





近期评论