NonLockingScrollView.java revision c3249d8f34388ec7f640b36b740119821fd6f322
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18package com.android.email.view;
19
20import android.content.Context;
21import android.util.AttributeSet;
22import android.view.MotionEvent;
23import android.widget.ScrollView;
24
25/**
26 * A {@link ScrollView} that will never lock scrolling in a particular direction.
27 *
28 * Usually ScrollView will capture all touch events once a drag has begun. In some cases,
29 * we want to delegate those touches to children as normal, even in the middle of a drag. This is
30 * useful when there are childviews like a WebView tha handles scrolling in the horizontal direction
31 * even while the ScrollView drags vertically.
32 *
33 * This is only tested to work for ScrollViews where the content scrolls in one direction.
34 */
35public class NonLockingScrollView extends ScrollView {
36    public NonLockingScrollView(Context context) {
37        super(context);
38    }
39    public NonLockingScrollView(Context context, AttributeSet attrs) {
40        super(context, attrs);
41    }
42    public NonLockingScrollView(Context context, AttributeSet attrs, int defStyle) {
43        super(context, attrs, defStyle);
44    }
45
46    /**
47     * Whether or not this view is in the middle of a drag.
48     */
49    private boolean mInDrag = false;
50
51    @Override
52    public boolean onInterceptTouchEvent(MotionEvent ev) {
53        final int action = ev.getActionMasked();
54        final boolean isUp = action == MotionEvent.ACTION_UP;
55
56        if (isUp && mInDrag) {
57            // An up event after a drag should be intercepted so that child views don't handle
58            // click events falsely after a drag.
59            mInDrag = false;
60            onTouchEvent(ev);
61            return true;
62        }
63
64        // Note the normal scrollview implementation is to intercept all touch events after it has
65        // detected a drag starting. We will handle this ourselves.
66        mInDrag = super.onInterceptTouchEvent(ev);
67        if (mInDrag) {
68            onTouchEvent(ev);
69        }
70
71        // Don't intercept events - pass them on to children as normal.
72        return false;
73    }
74}
75