|
1 | 1 | package cq.airbnb; |
2 | 2 |
|
3 | 3 | import android.content.Context; |
| 4 | +import android.graphics.Rect; |
4 | 5 | import android.support.design.widget.CoordinatorLayout; |
5 | 6 | import android.support.design.widget.TabLayout; |
| 7 | +import android.support.v4.view.GravityCompat; |
| 8 | +import android.support.v4.view.ViewCompat; |
6 | 9 | import android.util.AttributeSet; |
| 10 | +import android.view.Gravity; |
7 | 11 | import android.view.View; |
| 12 | +import android.view.ViewGroup; |
8 | 13 |
|
9 | 14 | import java.util.List; |
10 | 15 |
|
11 | | -import cq.behaviordemo.R; |
12 | | -import cq.behaviordemo.behavior.HeaderScrollingViewBehavior; |
13 | | - |
14 | 16 | /** |
15 | 17 | * 控制列表的移动 |
16 | 18 | */ |
17 | 19 |
|
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 | + |
20 | 25 | public ListBehavior(Context context, AttributeSet attrs) { |
21 | 26 | super(context, attrs); |
22 | | - mHeightToolbar=context.getResources().getDimensionPixelOffset(R.dimen.toolbar_height); |
23 | 27 | } |
24 | 28 |
|
25 | 29 |
|
| 30 | + /** |
| 31 | + * 计算宽高 |
| 32 | + */ |
26 | 33 | @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); |
30 | 51 |
|
| 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 | + } |
31 | 58 |
|
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 | + } |
36 | 79 | } |
37 | | - return null; |
| 80 | + return false; |
38 | 81 | } |
39 | 82 |
|
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); |
43 | 104 | } else { |
44 | | - return super.getScrollRange(v); |
| 105 | + // If we don't have a dependency, let super handle it |
| 106 | + super.onLayoutChild(parent, child, layoutDirection); |
45 | 107 | } |
| 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; |
46 | 123 | } |
47 | 124 |
|
48 | 125 | private boolean isDependOn(View dependency) { |
49 | 126 | return dependency instanceof TabLayout; |
50 | 127 | } |
51 | 128 |
|
| 129 | + private static int resolveGravity(int gravity) { |
| 130 | + return gravity == Gravity.NO_GRAVITY ? GravityCompat.START | Gravity.TOP : gravity; |
| 131 | + } |
| 132 | + |
| 133 | + |
52 | 134 | //跟随tab移动 |
53 | 135 | @Override |
54 | 136 | public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) { |
|
0 commit comments