Skip to content

Commit b5dc091

Browse files
committed
update
1 parent 64d9895 commit b5dc091

File tree

11 files changed

+150
-133
lines changed

11 files changed

+150
-133
lines changed

app/src/main/java/cq/airbnb/AirbnbActivity.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public class AirbnbActivity extends AppCompatActivity {
2727
findView();
2828
initView();
2929
initEvent();
30-
mTabLayout.setTranslationY(getResources().getDimensionPixelOffset(R.dimen.airbnb_translation_min));
3130
}
3231

3332
private void findView() {

app/src/main/java/cq/airbnb/BGBehavior.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public BGBehavior(Context context, AttributeSet attrs) {
2727

2828
@Override public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
2929
child.setTranslationY(dependency.getTranslationY()-mTranslationMax);
30-
3130
return true;
3231
}
3332
}

app/src/main/java/cq/airbnb/ListBehavior.java

Lines changed: 100 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,136 @@
11
package cq.airbnb;
22

33
import android.content.Context;
4+
import android.graphics.Rect;
45
import android.support.design.widget.CoordinatorLayout;
56
import android.support.design.widget.TabLayout;
7+
import android.support.v4.view.GravityCompat;
8+
import android.support.v4.view.ViewCompat;
69
import android.util.AttributeSet;
10+
import android.view.Gravity;
711
import android.view.View;
12+
import android.view.ViewGroup;
813

914
import java.util.List;
1015

11-
import cq.behaviordemo.R;
12-
import cq.behaviordemo.behavior.HeaderScrollingViewBehavior;
13-
1416
/**
1517
* 控制列表的移动
1618
*/
1719

18-
public class ListBehavior extends HeaderScrollingViewBehavior {
19-
private int mHeightToolbar;
20+
public class ListBehavior extends CoordinatorLayout.Behavior {
21+
22+
final Rect mTempRect1 = new Rect();
23+
final Rect mTempRect2 = new Rect();
24+
2025
public ListBehavior(Context context, AttributeSet attrs) {
2126
super(context, attrs);
22-
mHeightToolbar=context.getResources().getDimensionPixelOffset(R.dimen.toolbar_height);
2327
}
2428

2529

30+
/**
31+
* 计算宽高
32+
*/
2633
@Override
27-
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
28-
return isDependOn(dependency);
29-
}
34+
public boolean onMeasureChild(CoordinatorLayout parent, View child,
35+
int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec,
36+
int heightUsed) {
37+
final int childLpHeight = child.getLayoutParams().height;
38+
if (childLpHeight == ViewGroup.LayoutParams.MATCH_PARENT
39+
|| childLpHeight == ViewGroup.LayoutParams.WRAP_CONTENT) {
40+
// If the menu's height is set to match_parent/wrap_content then measure it
41+
// with the maximum visible height
42+
43+
final List<View> dependencies = parent.getDependencies(child);
44+
final View header = findFirstDependency(dependencies);
45+
if (header != null) {
46+
if (ViewCompat.getFitsSystemWindows(header)
47+
&& !ViewCompat.getFitsSystemWindows(child)) {
48+
// If the header is fitting system windows then we need to also,
49+
// otherwise we'll get CoL's compatible measuring
50+
ViewCompat.setFitsSystemWindows(child, true);
3051

52+
if (ViewCompat.getFitsSystemWindows(child)) {
53+
// If the set succeeded, trigger a new layout and return true
54+
child.requestLayout();
55+
return true;
56+
}
57+
}
3158

32-
@Override public View findFirstDependency(List<View> views) {
33-
for (View view : views) {
34-
if (isDependOn(view))
35-
return view;
59+
//可用的高度
60+
int availableHeight = View.MeasureSpec.getSize(parentHeightMeasureSpec);
61+
if (availableHeight == 0) {
62+
// If the measure spec doesn't specify a size, use the current height
63+
availableHeight = parent.getHeight();
64+
}
65+
66+
//列表高度为=可用高度-依赖的view的高度
67+
final int height = availableHeight - header.getMeasuredHeight();
68+
final int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(height,
69+
childLpHeight == ViewGroup.LayoutParams.MATCH_PARENT
70+
? View.MeasureSpec.EXACTLY
71+
: View.MeasureSpec.AT_MOST);
72+
73+
// Now measure the scrolling view with the correct height
74+
parent.onMeasureChild(child, parentWidthMeasureSpec,
75+
widthUsed, heightMeasureSpec, heightUsed);
76+
77+
return true;
78+
}
3679
}
37-
return null;
80+
return false;
3881
}
3982

40-
@Override protected int getScrollRange(View v) {
41-
if (isDependOn(v)) {
42-
return 0;
83+
@Override public boolean onLayoutChild(CoordinatorLayout parent, View child, int layoutDirection) {
84+
final List<View> dependencies = parent.getDependencies(child);
85+
final View header = findFirstDependency(dependencies);
86+
87+
if (header != null) {
88+
final CoordinatorLayout.LayoutParams lp =
89+
(CoordinatorLayout.LayoutParams) child.getLayoutParams();
90+
final Rect available = mTempRect1;
91+
//列表可用的 layoutParams . bottom 是为了 列表的高度为parent的可用高度
92+
available.set(parent.getPaddingLeft() + lp.leftMargin,
93+
header.getBottom() + lp.topMargin,
94+
parent.getWidth() - parent.getPaddingRight() - lp.rightMargin,
95+
parent.getHeight() + header.getBottom()
96+
- parent.getPaddingBottom() - lp.bottomMargin);
97+
98+
final Rect out = mTempRect2;
99+
//设置gravity
100+
GravityCompat.apply(resolveGravity(lp.gravity), child.getMeasuredWidth(),
101+
child.getMeasuredHeight(), available, out, layoutDirection);
102+
103+
child.layout(out.left, out.top, out.right, out.bottom);
43104
} else {
44-
return super.getScrollRange(v);
105+
// If we don't have a dependency, let super handle it
106+
super.onLayoutChild(parent, child, layoutDirection);
45107
}
108+
return true;
109+
}
110+
111+
@Override
112+
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
113+
return isDependOn(dependency);
114+
}
115+
116+
private View findFirstDependency(List<View> views) {
117+
for (View v : views) {
118+
if (isDependOn(v)) {
119+
return v;
120+
}
121+
}
122+
return null;
46123
}
47124

48125
private boolean isDependOn(View dependency) {
49126
return dependency instanceof TabLayout;
50127
}
51128

129+
private static int resolveGravity(int gravity) {
130+
return gravity == Gravity.NO_GRAVITY ? GravityCompat.START | Gravity.TOP : gravity;
131+
}
132+
133+
52134
//跟随tab移动
53135
@Override
54136
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {

app/src/main/java/cq/airbnb/SearchAllBehavior.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,14 @@ public SearchAllBehavior(Context context, AttributeSet attrs) {
3232
super(context, attrs);
3333
mMarginTop = context.getResources().getDimensionPixelOffset(R.dimen.airbnb_padding_content);
3434
mHeightUp = context.getResources().getDimensionPixelOffset(R.dimen.airbnb_up_height);
35+
mTranslationMin = context.getResources().getDimensionPixelOffset(R.dimen.airbnb_translation_min);
36+
mTranslationMax = context.getResources().getDimensionPixelOffset(R.dimen.airbnb_translation_max);
37+
mChangeHeight = mHeightUp - mMarginTop;
3538

36-
mTranslationMin =context.getResources().getDimensionPixelOffset(R.dimen.airbnb_translation_min);
37-
38-
mTranslationMax =context.getResources().getDimensionPixelOffset(R.dimen.airbnb_translation_max);
3939
}
4040

4141
@Override public boolean onLayoutChild(CoordinatorLayout parent, View child, int layoutDirection) {
4242
parent.onLayoutChild(child, layoutDirection);
43-
mChangeHeight = mHeightUp - mMarginTop;
44-
45-
4643
if (mTranslationGone == 0)
4744
mTranslationGone = -child.getRight();
4845
return true;
@@ -58,19 +55,14 @@ public SearchAllBehavior(Context context, AttributeSet attrs) {
5855
@Override public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
5956
float translationY = dependency.getTranslationY();
6057
if (translationY < mTranslationMin) {//向上移动的时候跟随tab
61-
if (child.getTranslationX() != 0) {
62-
child.setTranslationX(0);
63-
child.setClickable(true);
64-
}
58+
child.setTranslationX(0);
6559
child.setTranslationY(translationY - mTranslationMin);
6660
} else {//展开的时候,根据比例设置
6761
//tab 位移的比例
6862
float fraction = (translationY - mTranslationMin) / (mTranslationMax - mTranslationMin);
6963
//根据比例设置位移的距离
7064
if (fraction <= 1 / 3f) {
71-
if (child.getTranslationX() != 0) {
72-
child.setTranslationX(0);
73-
}
65+
child.setTranslationX(0);
7466
child.setTranslationY(fraction * mChangeHeight);
7567
child.setAlpha(1 - fraction * 3);
7668

app/src/main/java/cq/airbnb/SearchItemBehavior.java

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@ public SearchItemBehavior(Context context, AttributeSet attrs) {
3737
mMarginTop = context.getResources().getDimensionPixelOffset(R.dimen.airbnb_padding_content);
3838
mHeightUp = context.getResources().getDimensionPixelOffset(R.dimen.airbnb_up_height);
3939

40-
mTranslationMin =context.getResources().getDimensionPixelOffset(R.dimen.airbnb_translation_min);
41-
mTranslationMax =context.getResources().getDimensionPixelOffset(R.dimen.airbnb_translation_max);
40+
mTranslationMin = context.getResources().getDimensionPixelOffset(R.dimen.airbnb_translation_min);
41+
mTranslationMax = context.getResources().getDimensionPixelOffset(R.dimen.airbnb_translation_max);
42+
mChangeHeight = mHeightUp - mMarginTop;
43+
4244
}
4345

4446
@Override public boolean onLayoutChild(CoordinatorLayout parent, View child, int layoutDirection) {
4547
parent.onLayoutChild(child, layoutDirection);
46-
mChangeHeight = mHeightUp - mMarginTop;
4748

4849
mItemWhere = child.findViewById(R.id.lyt_where);
4950
mItemWhen = child.findViewById(R.id.lyt_when);
@@ -76,58 +77,37 @@ public SearchItemBehavior(Context context, AttributeSet attrs) {
7677

7778
//根据比例设置透明度
7879
if (fraction < 1 / 3f) {
79-
if (mItemWhere.getTranslationX() != 0) {
80-
mItemWhere.setTranslationX(0);
81-
}
82-
if (mItemWhen.getTranslationX() != mTranslationGone) {
83-
mItemWhen.setTranslationX(mTranslationGone);
80+
mItemWhere.setTranslationX(0);
81+
mItemWhen.setTranslationX(mTranslationGone);
8482

85-
}
86-
if (mItemWho.getTranslationX() != mTranslationGone) {
87-
mItemWho.setTranslationX(mTranslationGone);
88-
}
83+
mItemWho.setTranslationX(mTranslationGone);
8984

9085

9186
mItemWhere.setAlpha(fraction * 3);
9287
mItemWhen.setAlpha(0);
9388
mItemWho.setAlpha(0);
9489
} else if (fraction < 2 / 3f) {
95-
if (mItemWhere.getTranslationX() != 0) {
96-
mItemWhere.setTranslationX(0);
97-
}
98-
if (mItemWhen.getTranslationX() != 0) {
99-
mItemWhen.setTranslationX(0);
90+
mItemWhere.setTranslationX(0);
91+
mItemWhen.setTranslationX(0);
10092

101-
}
102-
if (mItemWho.getTranslationX() != mTranslationGone) {
103-
mItemWho.setTranslationX(mTranslationGone);
104-
}
93+
mItemWho.setTranslationX(mTranslationGone);
10594

10695
mItemWhere.setAlpha(1);
10796
mItemWhen.setAlpha(fraction * 3 - 1);
10897
mItemWho.setAlpha(0);
10998
} else {
110-
if (mItemWhere.getTranslationX() != 0) {
111-
mItemWhere.setTranslationX(0);
112-
}
113-
if (mItemWhen.getTranslationX() != 0) {
114-
mItemWhen.setTranslationX(0);
99+
mItemWhere.setTranslationX(0);
100+
mItemWhen.setTranslationX(0);
115101

116-
}
117-
if (mItemWho.getTranslationX() != 0) {
118-
mItemWho.setTranslationX(0);
119-
}
102+
mItemWho.setTranslationX(0);
120103

121104
mItemWhere.setAlpha(1);
122105
mItemWhen.setAlpha(1);
123106
mItemWho.setAlpha(fraction * 3 - 2);
124107
}
125-
} else {
126-
if (child.getTranslationX() != mTranslationGone) {
108+
} else if (child.getTranslationX() != mTranslationGone) {
127109
child.setTranslationX(mTranslationGone);
128110
child.setAlpha(0);
129-
}
130-
131111
}
132112
return true;
133113

0 commit comments

Comments
 (0)