Skip to content

Commit 46d1b57

Browse files
author
fengjian
committed
add DividerItemDecoration as RecylerView default ItemDecoration
1 parent b148932 commit 46d1b57

3 files changed

Lines changed: 152 additions & 3 deletions

File tree

app/src/main/java/com/jayfeng/lesscode/app/activity/adapterless/RecyclerAdapterActivity.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.jayfeng.lesscode.app.activity.adapterless;
22

33
import android.app.Activity;
4+
import android.graphics.Color;
5+
import android.graphics.drawable.ColorDrawable;
6+
import android.graphics.drawable.GradientDrawable;
47
import android.os.Bundle;
58
import android.support.v7.widget.LinearLayoutManager;
69
import android.support.v7.widget.RecyclerView;
@@ -9,7 +12,9 @@
912
import com.jayfeng.lesscode.app.R;
1013
import com.jayfeng.lesscode.app.model.Person;
1114
import com.jayfeng.lesscode.core.AdapterLess;
15+
import com.jayfeng.lesscode.core.DisplayLess;
1216
import com.jayfeng.lesscode.core.ViewLess;
17+
import com.jayfeng.lesscode.core.other.DividerItemDecoration;
1318

1419
import java.util.ArrayList;
1520
import java.util.List;
@@ -18,17 +23,20 @@ public class RecyclerAdapterActivity extends Activity {
1823

1924
List<Person> list;
2025
RecyclerView recyclerView;
21-
private RecyclerView.LayoutManager layoutManager;
26+
RecyclerView.LayoutManager layoutManager;
2227
RecyclerView.Adapter<AdapterLess.RecyclerViewHolder> adapter;
28+
DividerItemDecoration dividerItemDecoration;
2329

2430
@Override
2531
protected void onCreate(Bundle savedInstanceState) {
2632
super.onCreate(savedInstanceState);
2733
setContentView(R.layout.activity_adapter_recycler);
2834
recyclerView = ViewLess.$(this, R.id.recycler);
2935

30-
layoutManager = new LinearLayoutManager(this);
36+
layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
3137
recyclerView.setLayoutManager(layoutManager);
38+
dividerItemDecoration = new DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL_LIST, getResources().getDrawable(R.drawable.list_divider));
39+
dividerItemDecoration.setWidth(DisplayLess.$dp2px(16) + 1);
3240

3341
initData();
3442

@@ -44,6 +52,8 @@ public void onBindViewHolder(int position, AdapterLess.RecyclerViewHolder recycl
4452
});
4553

4654
recyclerView.setAdapter(adapter);
55+
56+
recyclerView.addItemDecoration(dividerItemDecoration);
4757
}
4858

4959
private void initData() {

app/src/main/res/layout/activity_main_list_item.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2-
android:layout_width="match_parent"
2+
android:layout_width="wrap_content"
33
android:layout_height="wrap_content">
44

55
<TextView
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package com.jayfeng.lesscode.core.other;
2+
3+
import android.content.Context;
4+
import android.content.res.TypedArray;
5+
import android.graphics.Canvas;
6+
import android.graphics.Rect;
7+
import android.graphics.drawable.Drawable;
8+
import android.support.v4.view.ViewCompat;
9+
import android.support.v7.widget.LinearLayoutManager;
10+
import android.support.v7.widget.RecyclerView;
11+
import android.view.View;
12+
13+
/**
14+
* RecyclerView的ItemDecoration的默认实现
15+
* 1. 默认使用系统的分割线
16+
* 2. 支持自定义Drawable类型
17+
* 3. 支持水平和垂直方向
18+
* 4. 修复了官方垂直Divider显示的bug
19+
* 扩展自官方android sdk下的Support7Demos下的DividerItemDecoration
20+
*/
21+
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
22+
23+
private static final int[] ATTRS = new int[]{
24+
android.R.attr.listDivider
25+
};
26+
27+
public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
28+
29+
public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
30+
31+
private Drawable mDivider;
32+
private int mWidth;
33+
private int mHeight;
34+
35+
private int mOrientation;
36+
37+
public DividerItemDecoration(Context context, int orientation) {
38+
final TypedArray a = context.obtainStyledAttributes(ATTRS);
39+
mDivider = a.getDrawable(0);
40+
a.recycle();
41+
setOrientation(orientation);
42+
}
43+
44+
/**
45+
* 新增:支持自定义dividerDrawable
46+
*
47+
* @param context
48+
* @param orientation
49+
* @param dividerDrawable
50+
*/
51+
public DividerItemDecoration(Context context, int orientation, Drawable dividerDrawable) {
52+
mDivider = dividerDrawable;
53+
setOrientation(orientation);
54+
}
55+
56+
public void setOrientation(int orientation) {
57+
if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
58+
throw new IllegalArgumentException("invalid orientation");
59+
}
60+
mOrientation = orientation;
61+
}
62+
63+
/**
64+
* 新增:支持手动为无高宽的drawable制定宽度
65+
* @param width
66+
*/
67+
public void setWidth(int width) {
68+
this.mWidth = width;
69+
}
70+
71+
/**
72+
* 新增:支持手动为无高宽的drawable制定高度
73+
* @param height
74+
*/
75+
public void setHeight(int height) {
76+
this.mHeight = height;
77+
}
78+
79+
@Override
80+
public void onDraw(Canvas c, RecyclerView parent) {
81+
if (mOrientation == VERTICAL_LIST) {
82+
drawVertical(c, parent);
83+
} else {
84+
drawHorizontal(c, parent);
85+
}
86+
}
87+
88+
public void drawVertical(Canvas c, RecyclerView parent) {
89+
final int left = parent.getPaddingLeft();
90+
final int right = parent.getWidth() - parent.getPaddingRight();
91+
92+
final int childCount = parent.getChildCount();
93+
for (int i = 0; i < childCount; i++) {
94+
final View child = parent.getChildAt(i);
95+
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
96+
.getLayoutParams();
97+
final int top = child.getBottom() + params.bottomMargin +
98+
Math.round(ViewCompat.getTranslationY(child));
99+
final int bottom = top + getDividerHeight();
100+
mDivider.setBounds(left, top, right, bottom);
101+
mDivider.draw(c);
102+
}
103+
}
104+
105+
public void drawHorizontal(Canvas c, RecyclerView parent) {
106+
final int top = parent.getPaddingTop();
107+
final int bottom = parent.getHeight() - parent.getPaddingBottom();
108+
109+
final int childCount = parent.getChildCount();
110+
for (int i = 0; i < childCount; i++) {
111+
final View child = parent.getChildAt(i);
112+
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
113+
.getLayoutParams();
114+
final int left = child.getRight() + params.rightMargin +
115+
Math.round(ViewCompat.getTranslationX(child));
116+
final int right = left + getDividerWidth();
117+
mDivider.setBounds(left, top, right, bottom);
118+
mDivider.draw(c);
119+
}
120+
}
121+
122+
@Override
123+
public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
124+
if (mOrientation == VERTICAL_LIST) {
125+
outRect.set(0, 0, 0, getDividerHeight());
126+
} else {
127+
outRect.set(0, 0, getDividerWidth(), 0);
128+
}
129+
}
130+
131+
private int getDividerWidth() {
132+
return mWidth > 0 ? mWidth : mDivider.getIntrinsicWidth();
133+
}
134+
135+
private int getDividerHeight() {
136+
return mHeight > 0 ? mHeight : mDivider.getIntrinsicHeight();
137+
}
138+
139+
}

0 commit comments

Comments
 (0)