[Android] 零碎知识汇总 - RecyclerView 顶部悬浮
16lz
2021-01-26
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); } }}
更多相关文章
- Android_绘制圆形图片
- android常用系统bar高度
- Android(安卓)基于surfaceView绘制正弦曲线
- android recycler添加分割线 点击事件等
- View 绘制机制 -- How Android(安卓)Draws Views
- Android屏幕简单 截图
- Android(安卓)Canvas绘制文字 自动换行
- 教你如何绘制圆形头像
- android view画图