1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.supportv7.widget.decorator; 18 19 import android.content.Context; 20 import android.content.res.TypedArray; 21 import android.graphics.Canvas; 22 import android.graphics.Rect; 23 import android.graphics.drawable.Drawable; 24 import android.support.v4.view.ViewCompat; 25 import android.support.v7.widget.LinearLayoutManager; 26 import android.support.v7.widget.RecyclerView; 27 import android.view.View; 28 29 public class DividerItemDecoration extends RecyclerView.ItemDecoration { 30 31 private static final int[] ATTRS = new int[]{ 32 android.R.attr.listDivider 33 }; 34 35 public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL; 36 37 public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL; 38 39 private Drawable mDivider; 40 41 private int mOrientation; 42 43 public DividerItemDecoration(Context context, int orientation) { 44 final TypedArray a = context.obtainStyledAttributes(ATTRS); 45 mDivider = a.getDrawable(0); 46 a.recycle(); 47 setOrientation(orientation); 48 } 49 50 public void setOrientation(int orientation) { 51 if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) { 52 throw new IllegalArgumentException("invalid orientation"); 53 } 54 mOrientation = orientation; 55 } 56 57 @Override 58 public void onDraw(Canvas c, RecyclerView parent) { 59 if (mOrientation == VERTICAL_LIST) { 60 drawVertical(c, parent); 61 } else { 62 drawHorizontal(c, parent); 63 } 64 } 65 66 public void drawVertical(Canvas c, RecyclerView parent) { 67 final int left = parent.getPaddingLeft(); 68 final int right = parent.getWidth() - parent.getPaddingRight(); 69 70 final int childCount = parent.getChildCount(); 71 for (int i = 0; i < childCount; i++) { 72 final View child = parent.getChildAt(i); 73 final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child 74 .getLayoutParams(); 75 final int top = child.getBottom() + params.bottomMargin + 76 Math.round(ViewCompat.getTranslationY(child)); 77 final int bottom = top + mDivider.getIntrinsicHeight(); 78 mDivider.setBounds(left, top, right, bottom); 79 mDivider.draw(c); 80 } 81 } 82 83 public void drawHorizontal(Canvas c, RecyclerView parent) { 84 final int top = parent.getPaddingTop(); 85 final int bottom = parent.getHeight() - parent.getPaddingBottom(); 86 87 final int childCount = parent.getChildCount(); 88 for (int i = 0; i < childCount; i++) { 89 final View child = parent.getChildAt(i); 90 final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child 91 .getLayoutParams(); 92 final int left = child.getRight() + params.rightMargin + 93 Math.round(ViewCompat.getTranslationX(child)); 94 final int right = left + mDivider.getIntrinsicHeight(); 95 mDivider.setBounds(left, top, right, bottom); 96 mDivider.draw(c); 97 } 98 } 99 100 @Override 101 public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) { 102 if (mOrientation == VERTICAL_LIST) { 103 outRect.set(0, 0, 0, mDivider.getIntrinsicHeight()); 104 } else { 105 outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0); 106 } 107 } 108 } 109