zihadrizkyef

DrawLine

Aug 2nd, 2016
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.32 KB | None | 0 0
  1. //DividerItemDecoration.java
  2. package info.androidhive.recyclerview;
  3.  
  4. import android.content.Context;
  5. import android.content.res.TypedArray;
  6. import android.graphics.Canvas;
  7. import android.graphics.Rect;
  8. import android.graphics.drawable.Drawable;
  9. import android.support.v7.widget.LinearLayoutManager;
  10. import android.support.v7.widget.RecyclerView;
  11. import android.view.View;
  12.  
  13. /**
  14.  * Created by Lincoln on 30/10/15.
  15.  */
  16. public class DividerItemDecoration extends RecyclerView.ItemDecoration {
  17.  
  18.     private static final int[] ATTRS = new int[]{
  19.             android.R.attr.listDivider
  20.     };
  21.  
  22.     public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
  23.  
  24.     public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
  25.  
  26.     private Drawable mDivider;
  27.  
  28.     private int mOrientation;
  29.  
  30.     public DividerItemDecoration(Context context, int orientation) {
  31.         final TypedArray a = context.obtainStyledAttributes(ATTRS);
  32.         mDivider = a.getDrawable(0);
  33.         a.recycle();
  34.         setOrientation(orientation);
  35.     }
  36.  
  37.     public void setOrientation(int orientation) {
  38.         if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
  39.             throw new IllegalArgumentException("invalid orientation");
  40.         }
  41.         mOrientation = orientation;
  42.     }
  43.  
  44.     @Override
  45.     public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
  46.         if (mOrientation == VERTICAL_LIST) {
  47.             drawVertical(c, parent);
  48.         } else {
  49.             drawHorizontal(c, parent);
  50.         }
  51.     }
  52.  
  53.     public void drawVertical(Canvas c, RecyclerView parent) {
  54.         final int left = parent.getPaddingLeft();
  55.         final int right = parent.getWidth() - parent.getPaddingRight();
  56.  
  57.         final int childCount = parent.getChildCount();
  58.         for (int i = 0; i < childCount; i++) {
  59.             final View child = parent.getChildAt(i);
  60.             final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
  61.                     .getLayoutParams();
  62.             final int top = child.getBottom() + params.bottomMargin;
  63.             final int bottom = top + mDivider.getIntrinsicHeight();
  64.             mDivider.setBounds(left, top, right, bottom);
  65.             mDivider.draw(c);
  66.         }
  67.     }
  68.  
  69.     public void drawHorizontal(Canvas c, RecyclerView parent) {
  70.         final int top = parent.getPaddingTop();
  71.         final int bottom = parent.getHeight() - parent.getPaddingBottom();
  72.  
  73.         final int childCount = parent.getChildCount();
  74.         for (int i = 0; i < childCount; i++) {
  75.             final View child = parent.getChildAt(i);
  76.             final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
  77.                     .getLayoutParams();
  78.             final int left = child.getRight() + params.rightMargin;
  79.             final int right = left + mDivider.getIntrinsicHeight();
  80.             mDivider.setBounds(left, top, right, bottom);
  81.             mDivider.draw(c);
  82.         }
  83.     }
  84.  
  85.     @Override
  86.     public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
  87.         if (mOrientation == VERTICAL_LIST) {
  88.             outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
  89.         } else {
  90.             outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
  91.         }
  92.     }
  93. }
Add Comment
Please, Sign In to add comment