1
+ package com .todou .nestrefresh ;
2
+
3
+ import android .content .Context ;
4
+ import android .content .res .TypedArray ;
5
+ import android .graphics .Canvas ;
6
+ import android .graphics .Color ;
7
+ import android .graphics .drawable .ColorDrawable ;
8
+ import android .support .annotation .NonNull ;
9
+ import android .support .annotation .Nullable ;
10
+ import android .support .design .widget .CoordinatorLayout ;
11
+ import android .util .AttributeSet ;
12
+ import android .view .View ;
13
+
14
+ public class RefreshBgView extends View implements CoordinatorLayout .AttachedBehavior {
15
+ private int mDrawBgHeight = 0 ;
16
+ private int mBgColor ;
17
+ private BgDrawable mBgDrawable ;
18
+
19
+ public RefreshBgView (Context context ) {
20
+ this (context , null );
21
+ }
22
+
23
+ public RefreshBgView (Context context , @ Nullable AttributeSet attrs ) {
24
+ this (context , attrs , 0 );
25
+ }
26
+
27
+
28
+ public RefreshBgView (Context context , @ Nullable AttributeSet attrs , int defStyleAttr ) {
29
+ super (context , attrs , defStyleAttr );
30
+
31
+ TypedArray a = context .obtainStyledAttributes (attrs , R .styleable .RefreshBgView );
32
+ this .mBgColor = a .getColor (R .styleable .RefreshBgView_refresh_bg_color , Color .WHITE );
33
+ a .recycle ();
34
+
35
+ mBgDrawable = new BgDrawable (mBgColor );
36
+ mBgDrawable .setMaxBottom (getBottom ());
37
+ setBackground (mBgDrawable );
38
+ }
39
+
40
+ @ Override
41
+ protected void onLayout (boolean changed , int left , int top , int right , int bottom ) {
42
+ super .onLayout (changed , left , top , right , bottom );
43
+ mBgDrawable .setMaxBottom (bottom );
44
+ }
45
+
46
+ @ NonNull
47
+ @ Override
48
+ public CoordinatorLayout .Behavior getBehavior () {
49
+ return new Behavior ();
50
+ }
51
+
52
+ protected static class BaseBehavior <T extends RefreshBgView > extends CoordinatorLayout .Behavior <T >{
53
+ public BaseBehavior () {
54
+ }
55
+
56
+ public BaseBehavior (Context context , AttributeSet attrs ) {
57
+ super (context , attrs );
58
+ }
59
+
60
+ @ Override
61
+ public boolean layoutDependsOn (CoordinatorLayout parent , T child , View dependency ) {
62
+ if (dependency instanceof RefreshBarLayout ) {
63
+ return true ;
64
+ }
65
+ return super .layoutDependsOn (parent , child , dependency );
66
+ }
67
+
68
+ @ Override
69
+ public boolean onDependentViewChanged (CoordinatorLayout parent , T child , View dependency ) {
70
+ if (dependency instanceof RefreshBarLayout ) {
71
+ RefreshBarLayout refreshBarLayout = (RefreshBarLayout ) dependency ;
72
+ int refreshHeaderHeight = refreshBarLayout .getRefreshHeaderOffset ();
73
+ int offset = refreshBarLayout .getTopBottomOffset ();
74
+ int preHeight = child .getLayoutParams ().height ;
75
+ int newHeight ;
76
+ if (offset > refreshHeaderHeight ) {
77
+ newHeight = offset ;
78
+ } else {
79
+ newHeight = 0 ;
80
+ }
81
+ if (preHeight != newHeight ) {
82
+ child .updateBgHeight (newHeight );
83
+ }
84
+ }
85
+ return false ;
86
+ }
87
+ }
88
+
89
+ @ Override
90
+ public void draw (Canvas canvas ) {
91
+ updateBgDrawable ();
92
+ super .draw (canvas );
93
+ }
94
+
95
+ private void updateBgDrawable () {
96
+ int height = mDrawBgHeight ;
97
+ int width = getWidth ();
98
+ if (getBackground () != null ) {
99
+ getBackground ().setBounds (0 , 0 , width , height );
100
+ }
101
+ }
102
+
103
+ public void updateBgHeight (int height ) {
104
+ mDrawBgHeight = height ;
105
+ invalidate ();
106
+ }
107
+
108
+ public static class BgDrawable extends ColorDrawable {
109
+ private int mMaxBottom = 0 ;
110
+
111
+ public BgDrawable (int color ) {
112
+ super (color );
113
+ }
114
+
115
+ public void setMaxBottom (int maxBottom ) {
116
+ mMaxBottom = maxBottom ;
117
+ }
118
+
119
+ @ Override
120
+ public void setBounds (int left , int top , int right , int bottom ) {
121
+ if (bottom >= mMaxBottom ) {
122
+ bottom = getBounds ().bottom ;
123
+ }
124
+ super .setBounds (left , top , right , bottom );
125
+ }
126
+ }
127
+
128
+ public static class Behavior extends BaseBehavior <RefreshBgView > {
129
+ public Behavior () {
130
+ }
131
+
132
+ public Behavior (Context context , AttributeSet attrs ) {
133
+ super (context , attrs );
134
+ }
135
+ }
136
+
137
+
138
+ }
0 commit comments