GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — 新增自定义万能分割线 · loosaSH/AndroidCustomView@1575262

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • app/src/main
    • java/com/allen/androidcustomview
      • activity
        • ItemCenterRVActivity.java
        • MainActivity.java
      • widget
        • SuperDividerItemDecoration.java
    • res/layout
      • adapter_item_main.xml
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎app/src/main/java/com/allen/androidcustomview/activity/ItemCenterRVActivity.java‎

Copy file name to clipboardExpand all lines: app/src/main/java/com/allen/androidcustomview/activity/ItemCenterRVActivity.java
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
import android.os.Bundle;
44
import android.support.annotation.Nullable;
55
import android.support.v7.app.AppCompatActivity;
6+
import android.support.v7.widget.DividerItemDecoration;
67
import android.support.v7.widget.LinearLayoutManager;
78
import android.support.v7.widget.LinearSnapHelper;
89
import android.support.v7.widget.RecyclerView;
910

1011
import com.allen.androidcustomview.R;
1112
import com.allen.androidcustomview.adapter.ItemCenter2Adapter;
1213
import com.allen.androidcustomview.adapter.ItemCenterAdapter;
13-
import com.allen.androidcustomview.widget.DividerItemDecoration;
1414
import com.allen.androidcustomview.widget.ItemCenterRecyclerView;
1515

1616
import java.util.ArrayList;
@@ -40,7 +40,6 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
4040
recyclerView = (ItemCenterRecyclerView) findViewById(R.id.recycler_view);
4141
recyclerView2 = (ItemCenterRecyclerView) findViewById(R.id.recycler_view_2);
4242

43-
4443
initRv(recyclerView);
4544
initRv(recyclerView2);
4645
recyclerView.setAdapter(adapter);
Collapse file

‎app/src/main/java/com/allen/androidcustomview/activity/MainActivity.java‎

Copy file name to clipboardExpand all lines: app/src/main/java/com/allen/androidcustomview/activity/MainActivity.java
+6-3Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import com.allen.androidcustomview.adapter.MainAdapter;
1212
import com.allen.androidcustomview.bean.TypeBean;
1313
import com.allen.androidcustomview.tagview.TagActivity;
14-
import com.allen.androidcustomview.widget.DividerItemDecoration;
14+
import com.allen.androidcustomview.widget.SuperDividerItemDecoration;
1515
import com.chad.library.adapter.base.BaseQuickAdapter;
1616

1717
import java.util.ArrayList;
@@ -32,12 +32,15 @@ protected void onCreate(Bundle savedInstanceState) {
3232
super.onCreate(savedInstanceState);
3333
setContentView(R.layout.activity_main);
3434

35-
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
35+
recyclerView = findViewById(R.id.recycler_view);
3636

3737
adapter = new MainAdapter(getData());
3838
adapter.setOnItemClickListener(this);
3939
recyclerView.setLayoutManager(new LinearLayoutManager(this));
40-
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
40+
recyclerView.addItemDecoration(new SuperDividerItemDecoration.Builder(this)
41+
.setDividerWidth(5)
42+
.setDividerColor(getResources().getColor(R.color.colorAccent))
43+
.build());
4144
recyclerView.setAdapter(adapter);
4245
}
4346

Collapse file

‎app/src/main/java/com/allen/androidcustomview/widget/SuperDividerItemDecoration.java‎

Copy file name to clipboard
+255Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
package com.allen.androidcustomview.widget;
2+
3+
import android.content.Context;
4+
import android.graphics.Canvas;
5+
import android.graphics.Paint;
6+
import android.graphics.Rect;
7+
import android.support.v7.widget.RecyclerView;
8+
import android.util.TypedValue;
9+
import android.view.View;
10+
import android.widget.LinearLayout;
11+
12+
13+
/**
14+
* <pre>
15+
* @author : Allen
16+
* e-mail : [email protected]
17+
* date : 2018/04/15
18+
* desc : 万能分割线
19+
* version : 1.0
20+
* </pre>
21+
*/
22+
public class SuperDividerItemDecoration extends RecyclerView.ItemDecoration {
23+
24+
25+
public static final int HORIZONTAL = LinearLayout.HORIZONTAL;
26+
public static final int VERTICAL = LinearLayout.VERTICAL;
27+
28+
private static Context context;
29+
30+
31+
/**
32+
* 默认分割线的颜色
33+
*/
34+
private int dividerDefaultColor = 0xFFE1E5E8;
35+
36+
/**
37+
* 分割线的颜色
38+
*/
39+
private int dividerColor;
40+
/**
41+
* 分割线的宽度
42+
*/
43+
private int dividerWidth;
44+
/**
45+
* 分割线距离左右两边的距离
46+
*/
47+
private int dividerPadding;
48+
/**
49+
* 分割线距离左边的距离
50+
*/
51+
private int dividerPaddingLeft;
52+
/**
53+
* 分割线距离右边的距离
54+
*/
55+
private int dividerPaddingRight;
56+
57+
/**
58+
* 分割线距离上边的距离
59+
*/
60+
private int dividerPaddingTop;
61+
/**
62+
* 分割线距离下边的距离
63+
*/
64+
private int dividerPaddingBottom;
65+
/**
66+
* 是否显示列表最后一条分割线
67+
*/
68+
private boolean dividerIsShowLastDivide;
69+
70+
71+
/**
72+
* 分割线item的画笔
73+
*/
74+
private Paint dividerPaint;
75+
76+
/**
77+
* 分割线开始的位置(解决recyclerView添加头布局的时候,要从header下边的position位置算起)
78+
*/
79+
private int dividerFromPosition = 0;
80+
81+
82+
/**
83+
* recyclerView布局方式(水平或者垂直)
84+
*/
85+
private int orientation;
86+
87+
88+
public SuperDividerItemDecoration(Builder builder) {
89+
90+
context = builder.context;
91+
92+
dividerColor = builder.dividerColor == 0 ? dividerDefaultColor : builder.dividerColor;
93+
dividerPadding = dp2px(builder.dividerPadding);
94+
dividerPaddingLeft = dp2px(builder.dividerPaddingLeft);
95+
dividerPaddingRight = dp2px(builder.dividerPaddingRight);
96+
dividerPaddingTop = dp2px(builder.dividerPaddingTop);
97+
dividerPaddingBottom = dp2px(builder.dividerPaddingBottom);
98+
dividerWidth = builder.dividerWidth == 0 ? dp2px(0.5f) : dp2px(builder.dividerWidth);
99+
dividerFromPosition = builder.dividerFromPosition;
100+
dividerIsShowLastDivide = builder.dividerIsShowLastDivide;
101+
orientation = builder.orientation;
102+
dividerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
103+
dividerPaint.setColor(dividerColor);
104+
105+
if (dividerPadding != 0) {
106+
dividerPaddingLeft = dividerPaddingRight = dividerPadding;
107+
dividerPaddingTop = dividerPaddingBottom = dividerPadding;
108+
}
109+
}
110+
111+
public static class Builder {
112+
113+
private Context context;
114+
private int dividerColor;
115+
private int dividerWidth;
116+
private int dividerPadding;
117+
private int dividerPaddingLeft;
118+
private int dividerPaddingRight;
119+
private int dividerPaddingTop;
120+
private int dividerPaddingBottom;
121+
private int dividerFromPosition;
122+
private boolean dividerIsShowLastDivide;
123+
private int orientation = VERTICAL;
124+
125+
public Builder(Context context) {
126+
this.context = context;
127+
}
128+
129+
public Builder setDividerColor(int dividerColor) {
130+
this.dividerColor = dividerColor;
131+
return this;
132+
}
133+
134+
public Builder setDividerWidth(int dividerWidth) {
135+
this.dividerWidth = dividerWidth;
136+
return this;
137+
}
138+
139+
public Builder setDividerPadding(int dividerPadding) {
140+
this.dividerPadding = dividerPadding;
141+
return this;
142+
}
143+
144+
public Builder setDividerPaddingLeft(int dividerPaddingLeft) {
145+
this.dividerPaddingLeft = dividerPaddingLeft;
146+
return this;
147+
}
148+
149+
public Builder setDividerPaddingRight(int dividerPaddingRight) {
150+
this.dividerPaddingRight = dividerPaddingRight;
151+
return this;
152+
}
153+
154+
public Builder setDividerPaddingTop(int dividerPaddingTop) {
155+
this.dividerPaddingTop = dividerPaddingTop;
156+
return this;
157+
}
158+
159+
public Builder setDividerPaddingBottom(int dividerPaddingBottom) {
160+
this.dividerPaddingBottom = dividerPaddingBottom;
161+
return this;
162+
}
163+
164+
public Builder setDividerFromPosition(int dividerFromPosition) {
165+
this.dividerFromPosition = dividerFromPosition;
166+
return this;
167+
}
168+
169+
public Builder setIsShowLastDivide(boolean dividerIsShowLastDivide) {
170+
this.dividerIsShowLastDivide = dividerIsShowLastDivide;
171+
return this;
172+
}
173+
174+
public Builder setOrientation(int orientation) {
175+
this.orientation = orientation;
176+
return this;
177+
}
178+
179+
public SuperDividerItemDecoration build() {
180+
return new SuperDividerItemDecoration(this);
181+
}
182+
}
183+
184+
@Override
185+
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
186+
super.onDraw(c, parent, state);
187+
188+
if (orientation == VERTICAL) {
189+
drawVertical(c, parent);
190+
} else {
191+
drawHorizontal(c, parent);
192+
}
193+
194+
}
195+
196+
197+
private void drawVertical(Canvas c, RecyclerView parent) {
198+
int count = parent.getChildCount();
199+
200+
if (count > 0) {
201+
int showCount = dividerIsShowLastDivide ? count : count - 1;
202+
for (int i = dividerFromPosition; i < showCount; i++) {
203+
View view = parent.getChildAt(i);
204+
//可见item的底部
205+
int itemBottom = view.getBottom();
206+
207+
c.drawRect(parent.getPaddingLeft() + dividerPaddingLeft,
208+
itemBottom,
209+
parent.getWidth() - parent.getPaddingRight() - dividerPaddingRight,
210+
itemBottom + dividerWidth,
211+
dividerPaint);
212+
}
213+
}
214+
}
215+
216+
private void drawHorizontal(Canvas c, RecyclerView parent) {
217+
int count = parent.getChildCount();
218+
if (count > 0) {
219+
int showCount = dividerIsShowLastDivide ? count : count - 1;
220+
for (int i = dividerFromPosition; i < showCount; i++) {
221+
View view = parent.getChildAt(i);
222+
//可见item的底部
223+
int itemRight = view.getRight();
224+
225+
c.drawRect(itemRight,
226+
parent.getPaddingTop() + dividerPaddingTop,
227+
itemRight + dividerWidth,
228+
parent.getHeight() - parent.getPaddingBottom() - dividerPaddingBottom,
229+
dividerPaint);
230+
}
231+
}
232+
}
233+
234+
235+
@Override
236+
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
237+
super.getItemOffsets(outRect, view, parent, state);
238+
if (orientation == VERTICAL) {
239+
outRect.bottom = dividerWidth;
240+
} else {
241+
outRect.right = dividerWidth;
242+
}
243+
}
244+
245+
/**
246+
* dp 2 px
247+
*
248+
* @param dpVal
249+
*/
250+
public static int dp2px(float dpVal) {
251+
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
252+
dpVal, context.getResources().getDisplayMetrics());
253+
}
254+
255+
}
Collapse file

‎app/src/main/res/layout/adapter_item_main.xml‎

Copy file name to clipboardExpand all lines: app/src/main/res/layout/adapter_item_main.xml
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
android:orientation="vertical">
66

77
<TextView
8-
android:padding="15dp"
98
android:id="@+id/title_tv"
109
android:layout_width="match_parent"
1110
android:layout_height="wrap_content"
12-
android:gravity="center_vertical" />
11+
android:gravity="center_vertical"
12+
android:padding="15dp"
13+
android:textSize="16sp" />
1314
</LinearLayout>

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.