Skip to content

Commit 6ced253

Browse files
committed
add refresh bg view
1 parent e8a50b7 commit 6ced253

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed

nestrefresh/src/main/java/com/todou/nestrefresh/RefreshBarLayout.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ class RefreshBarLayout @JvmOverloads constructor(
9898
return lastInsets?.systemWindowInsetTop ?: 0
9999
}
100100

101+
fun getTopBottomOffset(): Int {
102+
return refreshBarBehavior?.getTopAndBottomOffset() ?: 0
103+
}
104+
101105
private fun updateAllChildList() {
102106
childScrollAbleList = getScrollableChild(this)
103107
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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+
}

nestrefresh/src/main/res/values/attrs.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
<attr name="behavior_maxRange" format="dimension"/>
1818
</declare-styleable>
1919

20+
<declare-styleable name="RefreshBgView">
21+
<attr name="refresh_bg_color" format="color"/>
22+
</declare-styleable>
23+
2024
<declare-styleable name="NRCollapsingToolbarLayout">
2125
<!-- Specifies extra space on the start, top, end and bottom
2226
sides of the the expanded title text. Margin values should be positive. -->

0 commit comments

Comments
 (0)