package com.example.dq.app_itemdecoration;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.RecyclerView;import android.text.TextPaint;import android.text.TextUtils;import android.view.View;/** * Created by DQ on 2017/8/7 * 城市列表的分割线, 主要用于绘制字母分组 */public class CityItemDecoration extends RecyclerView.ItemDecoration {  private int mGroupHeight = 60;  private int mSideMargin = 10;  private Paint mGroupPaint;  private TextPaint mTextPaint;  private GroupListener mListener;  public CityItemDecoration(Context context, GroupListener pListener) {    mGroupPaint = new Paint(Paint.ANTI_ALIAS_FLAG);    mGroupPaint.setColor(context.getResources().getColor(android.R.color.holo_blue_dark));    mListener = pListener;    mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);    mTextPaint.setTextSize(40);    mTextPaint.setColor(Color.WHITE);    mTextPaint.setTextAlign(Paint.Align.LEFT);  }  // 偏移距离,可以变相的理解成,分割线的高度  @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {    super.getItemOffsets(outRect, view, parent, state);    int position = parent.getChildAdapterPosition(view);    // 不是省份    if (mListener == null || TextUtils.isEmpty(mListener.getGroupName(position))) {      return;    }    // 是否显示 省份标签    if (isFirstInGroup(position)) {      outRect.top = mGroupHeight;    }  }  @Override public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {    super.onDrawOver(c, parent, state);    final int itemCount = state.getItemCount();    final int childCount = parent.getChildCount();    final int left = parent.getLeft() + parent.getPaddingLeft();    final int right = parent.getRight() + parent.getPaddingRight();    String preGroupName; // 标记上一个item的省份    String currentGroupName = null; // 当前item的省份    for (int i = 0; i < childCount; i++) {      View child = parent.getChildAt(i);      int position = parent.getChildAdapterPosition(child);      preGroupName = currentGroupName;      currentGroupName = mListener.getGroupName(position);      // 普通城市 or 和上一次省份相同      if (TextUtils.equals(currentGroupName, preGroupName)) {        continue;      }      // 计算高度      int bottom = Math.max(mGroupHeight, child.getTop());      if (position < itemCount - 1) {        String nextGroupName = mListener.getGroupName(position + 1);        int viewBottom = child.getBottom();        if (!currentGroupName.equals(nextGroupName) && viewBottom < bottom) {          bottom = viewBottom;        }      }      // 接下来, 就是省份的绘制了      c.drawRect(left, bottom - mGroupHeight, right, bottom, mGroupPaint);      // 绘制文字      Paint.FontMetrics fm = mTextPaint.getFontMetrics();      float baseline = bottom - (mGroupHeight - (fm.bottom - fm.top)) / 2 - fm.bottom;      // TODO: 2017/8/7 DQ: 这里添加的是左右边距,是可以自己设置的      float textWidth = mTextPaint.measureText(currentGroupName);      float marginLeft = true ? 0 : right - textWidth;      mSideMargin = Math.abs(mSideMargin);      mSideMargin = true ? mSideMargin : -mSideMargin;      c.drawText(currentGroupName, left + mSideMargin + marginLeft, baseline, mTextPaint);    }  }  public interface GroupListener {    String getGroupName(int position);  }  private boolean isFirstInGroup(int position) {    if (position == 0) {      return true;    } else {      String prevGroupId = mListener.getGroupName(position - 1);      String groupId = mListener.getGroupName(position);      return !TextUtils.equals(prevGroupId, groupId);    }  }}

更多相关文章

  1. Android_绘制圆形图片
  2. android常用系统bar高度
  3. Android(安卓)基于surfaceView绘制正弦曲线
  4. android recycler添加分割线 点击事件等
  5. View 绘制机制 -- How Android(安卓)Draws Views
  6. Android屏幕简单 截图
  7. Android(安卓)Canvas绘制文字 自动换行
  8. 教你如何绘制圆形头像
  9. android view画图

随机推荐

  1. scrollbarsstyle
  2. Android(安卓)侧滑(双向滑动菜单)效果
  3. Android源码分析(四)-----Android源码编译
  4. ListView使用技巧
  5. Android SharedPreferences和File
  6. android中SharedPreferences和Preference
  7. ANDROID模拟器不能启动
  8. android实现开机欢迎界面
  9. 【学习笔记】android基本环境
  10. android线程调度工具类SchedulerUtils