-
Notifications
You must be signed in to change notification settings - Fork 274
/
ItemDecoration.java
104 lines (83 loc) · 3.57 KB
/
ItemDecoration.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
package com.vivian.timeline.timeline1;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import com.vivian.timeline.R;
/**
* * _ _
* * __ _(_)_ _(_) __ _ _ __
* * \ \ / / \ \ / / |/ _` | '_ \
* * \ V /| |\ V /| | (_| | | | |
* * \_/ |_| \_/ |_|\__,_|_| |_|
* <p>
* Created by vivian on 2017/6/9.
*/
public class ItemDecoration extends RecyclerView.ItemDecoration {
private Context mContext;
private int distance;
Drawable drawable;
Drawable verticalLine;
Drawable horizontalLine;
public ItemDecoration(Context context, int distance) {
mContext = context;
this.distance = distance;
verticalLine = ContextCompat.getDrawable(mContext, R.drawable.gray_line);
drawable = ContextCompat.getDrawable(mContext, R.drawable.time);
horizontalLine = ContextCompat.getDrawable(mContext, R.drawable.horizontal_line);
}
@Override
public void getItemOffsets(Rect outRect, @NonNull View view, RecyclerView parent, @NonNull RecyclerView.State state) {
outRect.left = distance;
outRect.right = distance;
outRect.bottom = 3 * distance;
if (parent.getChildAdapterPosition(view) == 0) {
outRect.top = distance;
} else if (parent.getChildAdapterPosition(view) == 1) {
outRect.top = 4 * distance;
}
if (parent.getChildAdapterPosition(view) % 2 == 0) {
outRect.left = 20;
} else {
outRect.right = 20;
}
}
@Override
public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
drawHorizontal(c, parent);
drawVertical(c, parent);
}
public void drawHorizontal(Canvas c, RecyclerView parent) {
final int top = parent.getPaddingTop();
final int bottom = parent.getHeight() - parent.getPaddingBottom();
final int parentWidth = parent.getMeasuredWidth();
verticalLine.setBounds(parentWidth / 2 - 1, top, parentWidth / 2 + 1,bottom);
verticalLine.draw(c);
}
public void drawVertical(Canvas c, RecyclerView parent) {
final int parentWidth = parent.getMeasuredWidth();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {//-1最后一个不画
final View child = parent.getChildAt(i);
final int top = child.getTop() + (child.getBottom() - child.getTop()) / 2 - drawable.getIntrinsicHeight() / 2;
final int bottom = top + drawable.getIntrinsicHeight();
int horizontalLineLeft = child.getRight();
int horizontalLineRight = parentWidth / 2;
int horizontalLineTop = child.getTop() + (child.getBottom() - child.getTop()) / 2;
if (child.getLeft()<parentWidth/2) {
horizontalLineLeft = parentWidth / 2;
horizontalLineRight = child.getLeft();
}
horizontalLine.setBounds(horizontalLineLeft, horizontalLineTop, horizontalLineRight, horizontalLineTop + 2);
horizontalLine.draw(c);
int drawableLeft = parentWidth / 2 - drawable.getIntrinsicWidth() / 2;
int drawableRight = parentWidth / 2 + drawable.getIntrinsicWidth() / 2;
drawable.setBounds(drawableLeft, top, drawableRight, bottom);
drawable.draw(c);
}
}
}