Advertisement
r4j

SimpleDividerItemDecoration

r4j
Mar 16th, 2017
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.51 KB | None | 0 0
  1.  
  2.  
  3. import android.annotation.SuppressLint;
  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.annotation.NonNull;
  10. import android.support.v4.view.ViewCompat;
  11. import android.support.v7.widget.LinearLayoutManager;
  12. import android.support.v7.widget.RecyclerView;
  13. import android.view.View;
  14. import android.widget.LinearLayout;
  15.  
  16. public class SimpleDividerItemDecoration extends RecyclerView.ItemDecoration {
  17.     public static final int HORIZONTAL = LinearLayout.HORIZONTAL;
  18.     public static final int VERTICAL = LinearLayout.VERTICAL;
  19.  
  20.     private static final int[] ATTRS = new int[]{ android.R.attr.listDivider };
  21.  
  22.     private Drawable mDivider;
  23.  
  24.     /**
  25.      * Current orientation. Either {@link #HORIZONTAL} or {@link #VERTICAL}.
  26.      */
  27.     private int mOrientation;
  28.  
  29.     private final Rect mBounds = new Rect();
  30.  
  31.     /**
  32.      * Creates a divider {@link RecyclerView.ItemDecoration} that can be used with a
  33.      * {@link LinearLayoutManager}.
  34.      *
  35.      * @param context Current context, it will be used to access resources.
  36.      * @param orientation Divider orientation. Should be {@link #HORIZONTAL} or {@link #VERTICAL}.
  37.      */
  38.     public SimpleDividerItemDecoration(Context context, int orientation) {
  39.         final TypedArray a = context.obtainStyledAttributes(ATTRS);
  40.         mDivider = a.getDrawable(0);
  41.         a.recycle();
  42.         setOrientation(orientation);
  43.     }
  44.  
  45.     /**
  46.      * Sets the orientation for this divider. This should be called if
  47.      * {@link RecyclerView.LayoutManager} changes orientation.
  48.      *
  49.      * @param orientation {@link #HORIZONTAL} or {@link #VERTICAL}
  50.      */
  51.     public void setOrientation(int orientation) {
  52.         if (orientation != HORIZONTAL && orientation != VERTICAL) {
  53.             throw new IllegalArgumentException(
  54.                     "Invalid orientation. It should be either HORIZONTAL or VERTICAL");
  55.         }
  56.         mOrientation = orientation;
  57.     }
  58.  
  59.     /**
  60.      * Sets the {@link Drawable} for this divider.
  61.      *
  62.      * @param drawable Drawable that should be used as a divider.
  63.      */
  64.     public void setDrawable(@NonNull Drawable drawable) {
  65.         if (drawable == null) {
  66.             throw new IllegalArgumentException("Drawable cannot be null.");
  67.         }
  68.         mDivider = drawable;
  69.     }
  70.  
  71.     @Override
  72.     public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
  73.         if (parent.getLayoutManager() == null) {
  74.             return;
  75.         }
  76.         if (mOrientation == VERTICAL) {
  77.             drawVertical(c, parent);
  78.         } else {
  79.             drawHorizontal(c, parent);
  80.         }
  81.     }
  82.  
  83.     @SuppressLint("NewApi")
  84.     private void drawVertical(Canvas canvas, RecyclerView parent) {
  85.         canvas.save();
  86.         final int left;
  87.         final int right;
  88.         if (parent.getClipToPadding()) {
  89.             left = parent.getPaddingLeft();
  90.             right = parent.getWidth() - parent.getPaddingRight();
  91.             canvas.clipRect(left, parent.getPaddingTop(), right,
  92.                     parent.getHeight() - parent.getPaddingBottom());
  93.         } else {
  94.             left = 0;
  95.             right = parent.getWidth();
  96.         }
  97.  
  98.         final int childCount = parent.getChildCount();
  99.         for (int i = 0; i < childCount; i++) {
  100.             if(!enableDividerForPosition(i)){
  101.                 continue;
  102.             }
  103.  
  104.             final View child = parent.getChildAt(i);
  105.             parent.getDecoratedBoundsWithMargins(child, mBounds);
  106.             final int bottom = mBounds.bottom + Math.round(ViewCompat.getTranslationY(child));
  107.             final int top = bottom - mDivider.getIntrinsicHeight();
  108.             mDivider.setBounds(left, top, right, bottom);
  109.             mDivider.draw(canvas);
  110.         }
  111.         canvas.restore();
  112.     }
  113.  
  114.     @SuppressLint("NewApi")
  115.     private void drawHorizontal(Canvas canvas, RecyclerView parent) {
  116.         canvas.save();
  117.         final int top;
  118.         final int bottom;
  119.         if (parent.getClipToPadding()) {
  120.             top = parent.getPaddingTop();
  121.             bottom = parent.getHeight() - parent.getPaddingBottom();
  122.             canvas.clipRect(parent.getPaddingLeft(), top,
  123.                     parent.getWidth() - parent.getPaddingRight(), bottom);
  124.         } else {
  125.             top = 0;
  126.             bottom = parent.getHeight();
  127.         }
  128.  
  129.         final int childCount = parent.getChildCount();
  130.         for (int i = 0; i < childCount; i++) {
  131.             if(!enableDividerForPosition(i)){
  132.                 continue;
  133.             }
  134.  
  135.             final View child = parent.getChildAt(i);
  136.             parent.getLayoutManager().getDecoratedBoundsWithMargins(child, mBounds);
  137.             final int right = mBounds.right + Math.round(ViewCompat.getTranslationX(child));
  138.             final int left = right - mDivider.getIntrinsicWidth();
  139.             mDivider.setBounds(left, top, right, bottom);
  140.             mDivider.draw(canvas);
  141.         }
  142.         canvas.restore();
  143.     }
  144.  
  145.     @Override
  146.     public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
  147.                                RecyclerView.State state) {
  148.         if (mOrientation == VERTICAL) {
  149.             outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
  150.         } else {
  151.             outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
  152.         }
  153.     }
  154.  
  155.     public boolean enableDividerForPosition(int position){
  156.         return true;
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement