forked from syupo/pinned
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScrollViewWithPinnedView.java
105 lines (90 loc) · 2.45 KB
/
ScrollViewWithPinnedView.java
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
97
98
99
100
101
102
103
104
105
package com.example.window;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;
/**
* ScrollViewWithPinnedView
*
* @author lxp
* @date 2013-8-7
*/
public class ScrollViewWithPinnedView extends ScrollView {
private static final String TAG = "ScrollViewWithPinnedView";
/** Pinned View */
private View mPinnedView;
/** The Width of Pinned View */
private int mPinnedViewWidth;
/** The Heigh of Pinned View */
private int mPinnedViewHeight;
/** The Top Position of Pinned View */
private int mPinnedViewOriginalTop;
/**
* 构造方法
*
* @param context
*/
public ScrollViewWithPinnedView(Context context) {
super(context);
}
/**
* 构造方法
*
* @param context
* @param attrs
*/
public ScrollViewWithPinnedView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* @param view
*/
public void setPinnedView(View view) {
this.mPinnedView = view;
requestLayout();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Log.d(TAG, "onMeasure");
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (mPinnedView != null) {
// 测量mPinnedView的尺寸大小
measureChild(mPinnedView, widthMeasureSpec, heightMeasureSpec);
mPinnedViewWidth = mPinnedView.getMeasuredWidth();
mPinnedViewHeight = mPinnedView.getMeasuredHeight();
// 计算mPinnedView距离它的父容器顶部距离
mPinnedViewOriginalTop = mPinnedView.getTop();
}
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Log.d(TAG, "onLayout");
super.onLayout(changed, left, top, right, bottom);
}
@Override
protected void dispatchDraw(Canvas canvas) {
Log.d(TAG, "dispatchDraw");
super.dispatchDraw(canvas);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Log.d(TAG, "onScrollChanged");
super.onScrollChanged(l, t, oldl, oldt);
if (mPinnedView == null) {
return;
}
if (mPinnedViewOriginalTop <= t) {
mPinnedView.layout(0, t, mPinnedViewWidth, mPinnedViewHeight + t);
} else {
mPinnedView.layout(0, mPinnedViewOriginalTop, mPinnedViewWidth, mPinnedViewOriginalTop + mPinnedViewHeight);
}
mPinnedView.bringToFront();
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
return super.onTouchEvent(ev);
}
};