Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright 2017 Google Inc.
      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 package com.example.android.wearable.wear.messaging.util;
     17 
     18 import android.content.Context;
     19 import android.graphics.Canvas;
     20 import android.graphics.drawable.Drawable;
     21 import android.support.v4.content.ContextCompat;
     22 import android.support.v7.widget.RecyclerView;
     23 import android.view.View;
     24 
     25 /** Recycler view divider. */
     26 public class DividerItemDecoration extends RecyclerView.ItemDecoration {
     27 
     28     private Drawable mDrawable;
     29 
     30     public DividerItemDecoration(Context context, int resId) {
     31         mDrawable = ContextCompat.getDrawable(context, resId);
     32     }
     33 
     34     @Override
     35     public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
     36         int left = parent.getLeft();
     37         int right = parent.getRight();
     38 
     39         int childCount = parent.getChildCount();
     40 
     41         for (int i = 0; i < childCount; i++) {
     42             View child = parent.getChildAt(i);
     43 
     44             int top = child.getBottom();
     45             int bottom = top + mDrawable.getIntrinsicHeight();
     46 
     47             mDrawable.setBounds(left, top, right, bottom);
     48             mDrawable.draw(c);
     49         }
     50     }
     51 }
     52