- package com.tal.jxtteacher.uijxt.weight;
-
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Rect;
- import android.support.v7.widget.LinearLayoutManager;
- import android.support.v7.widget.RecyclerView;
- import android.support.v7.widget.RecyclerView.ItemDecoration;
- import android.view.View;
-
- import com.tal.jxtteacher.uijxt.adapter.RecyclerViewAdapter;
-
- public class CreatGroupPickContactsItemDecoration extends ItemDecoration {
-
-
- //头部的高
- private int mItemHeaderHeight;
- private int mTextPaddingLeft;
-
- //画笔,绘制头部和分割线
- private Paint mItemHeaderPaint;
- private Paint mTextPaint;
- private Paint mLinePaint;
-
- private Rect mTextRect;
-
- private Context context;
-
- public CreatGroupPickContactsItemDecoration(Context context) {
- this.context = context;
- mItemHeaderHeight = dp2px(context, 40);
- mTextPaddingLeft = dp2px(context, 0);
-
- mTextRect = new Rect();
-
- mItemHeaderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
- mItemHeaderPaint.setColor(Color.WHITE);
-
- mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
- mTextPaint.setTextSize(46);
- mTextPaint.setColor(Color.parseColor("#979797"));
-
- mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
- mLinePaint.setStyle(Paint.Style.FILL);
- mLinePaint.setColor(Color.parseColor("#EFEFEF"));
-
- }
-
- /**
- * 绘制Item的分割线和组头
- *
- * @param c
- * @param parent
- * @param state
- */
- @Override
- public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
- if (parent.getAdapter() instanceof RecyclerViewAdapter) {
- RecyclerViewAdapter adapter = (RecyclerViewAdapter) parent.getAdapter();
- int count = parent.getChildCount();//获取可见范围内Item的总数
- for (int i = 0; i < count; i++) {
- View view = parent.getChildAt(i);
- int position = parent.getChildLayoutPosition(view);
- boolean isHeader = adapter.isItemHeader(position);
- int left = parent.getPaddingLeft();
- int right = parent.getWidth() - parent.getPaddingRight();
- if (isHeader) {
- c.drawRect(left, view.getTop() - mItemHeaderHeight, right, view.getTop(), mItemHeaderPaint);
- mTextPaint.getTextBounds(adapter.getGroupName(position), 0, adapter.getGroupName(position).length(), mTextRect);
- c.drawText(adapter.getGroupName(position), left + mTextPaddingLeft, (view.getTop() - mItemHeaderHeight) + mItemHeaderHeight / 2 + mTextRect.height() / 2, mTextPaint);
- c.drawRect(left,view.getTop()-mItemHeaderHeight,right,view.getTop()-mItemHeaderHeight+dp2px(context,1),mLinePaint);
- c.drawRect(left,view.getTop() - dp2px(context,1),right,view.getTop(),mLinePaint);
- } else {
- c.drawRect(left, view.getTop() - dp2px(context,1), right, view.getTop(), mLinePaint);
- }
- }
- }
- }
-
-
- /**
- * 绘制Item的顶部布局(吸顶效果)
- *
- * @param c
- * @param parent
- * @param state
- */
- @Override
- public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
- if (parent.getAdapter() instanceof RecyclerViewAdapter) {
- RecyclerViewAdapter adapter = (RecyclerViewAdapter) parent.getAdapter();
- int position = ((LinearLayoutManager) (parent.getLayoutManager())).findFirstVisibleItemPosition();
- if (position < 0){
- return;
- }
- View view = parent.findViewHolderForAdapterPosition(position).itemView;
- boolean isHeader = adapter.isItemHeader(position + 1);
- int top = parent.getPaddingTop();
- int left = parent.getPaddingLeft();
- int right = parent.getWidth() - parent.getPaddingRight();
- if (isHeader) {
- int bottom = Math.min(mItemHeaderHeight, view.getBottom());
- c.drawRect(left, top + view.getTop() - mItemHeaderHeight, right, top + bottom, mItemHeaderPaint);
- mTextPaint.getTextBounds(adapter.getGroupName(position), 0, adapter.getGroupName(position).length(), mTextRect);
- c.drawText(adapter.getGroupName(position), left + mTextPaddingLeft, top + mItemHeaderHeight / 2 + mTextRect.height() / 2 - (mItemHeaderHeight - bottom), mTextPaint);
- } else {
- c.drawRect(left, top, right, top + mItemHeaderHeight, mItemHeaderPaint);
- mTextPaint.getTextBounds(adapter.getGroupName(position), 0, adapter.getGroupName(position).length(), mTextRect);
- c.drawText(adapter.getGroupName(position), left + mTextPaddingLeft, top + mItemHeaderHeight / 2 + mTextRect.height() / 2, mTextPaint);
- }
- c.save();
- }
-
- }
-
- /**
- * 设置Item的间距
- *
- * @param outRect
- * @param view
- * @param parent
- * @param state
- */
- @Override
- public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
- if (parent.getAdapter() instanceof RecyclerViewAdapter) {
- RecyclerViewAdapter adapter = (RecyclerViewAdapter) parent.getAdapter();
- int position = parent.getChildLayoutPosition(view);
- boolean isHeader = adapter.isItemHeader(position);
- if (isHeader) {
- outRect.top = mItemHeaderHeight;
- } else {
- outRect.top = dp2px(context,1);
- }
- }
- }
-
-
- /**
- * dp转换成px
- */
- private int dp2px(Context context, float dpValue) {
- float scale = context.getResources().getDisplayMetrics().density;
- return (int) (dpValue * scale + 0.5f);
- }
-
- }