summaryrefslogtreecommitdiffstats
path: root/src/org/cyanogenmod/theme/util/ChooserDetailScrollView.java
blob: 8734cae7bffa4da0efac3cffd695f839c72aa181 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
 * Copyright (C) 2014 The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.cyanogenmod.theme.util;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;

/**
 * A custom ScrollView capable of alerting its children when a scroll has changed.
 *
 * This custom view will also measure its children differently. Normally a child in scrollview
 * receives an UNSPECIFIED MeasureSpec so that the child will size itself as large as it needs to be.
 *
 * In our case we want to have a unique layout where the first full page of the scrollview
 * has very particular content
 * ie the "drawer title/author", and the viewpager taking the remaining part of the screen", with the
 * theme component checkboxes taking as much space as it needs.
 *
 **/
public class ChooserDetailScrollView extends ScrollView {
    private float xDistance;
    private float yDistance;
    private float lastX;
    private float lastY;

    public ChooserDetailScrollView(Context context) {
        super(context);
    }

    public ChooserDetailScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ChooserDetailScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed,
                                           int parentHeightMeasureSpec, int heightUsed) {
        final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();

        final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
                mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin
                        + widthUsed, lp.width);
        child.measure(childWidthMeasureSpec, parentHeightMeasureSpec);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                xDistance = yDistance = 0f;
                lastX = ev.getX();
                lastY = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                final float curX = ev.getX();
                final float curY = ev.getY();
                xDistance = Math.abs(curX - lastX);
                yDistance = Math.abs(curY - lastY);
                lastX = curX;
                lastY = curY;
                if(xDistance > yDistance) return false;
        }

        return super.onInterceptTouchEvent(ev);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        ((ChooserDetailLinearLayout)getChildAt(0)).onScrollChanged(l, t, oldl, oldt);
    }

    public View getHandle() {
        return ((ChooserDetailLinearLayout)getChildAt(0)).getHandle();
    }
}