안녕세계
[안드로이드] RecyclerView - Divider 생성 본문
[안드로이드] RecyclerView - Divider 생성
Junhong Kim 2017. 1. 16. 01:44728x90
반응형
[ Step1 ] 선(Line)을 만들기 위한 ItemDecoration Class 생성
[java/SimpleDividerItemDecoration.java]
public class SimpleDividerItemDecoration extends RecyclerView.ItemDecoration {
private Drawable mDivider;
public SimpleDividerItemDecoration(Context context) {
mDivider = context.getResources().getDrawable(R.drawable.line_divider);
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int top = child.getBottom() + params.bottomMargin;
int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
}
[Step2] Divider 색상 지정
[res/drawable/line_divider.xml]
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
android:width="1dp"
android:height="1dp" />
<solid android:color="#000000" />
</shape>
[Step3] RecyclerView에 Divider 지정
processingList.addItemDecoration(new SimpleDividerItemDecoration(getContext()));
※ Note: adapter를 Setting 전에 item decoration을 추가해줘야 합니다
※ 참고
[1] https://gist.github.com/polbins/e37206fbc444207c0e92
728x90
반응형
'Client > Android' 카테고리의 다른 글
[안드로이드] Splash - 앱 로딩 화면 (0) | 2017.03.20 |
---|---|
[안드로이드] Toolbar (0) | 2017.01.17 |
[안드로이드] RecyclerView With TabLayout (Feat. ViewPager) (0) | 2017.01.12 |
[라이브러리] Buttef Knife 8.4.0 (0) | 2016.11.25 |
[안드로이드] SimpleDateFormat (날짜형식 변경) (0) | 2016.11.01 |
Comments