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: 這里添加的是左右邊距,是可以自己設(shè)置的
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] 零碎知識匯總 - RecyclerView 頂部懸浮
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 用這個方式實現(xiàn)有很大的局限性, 粗糙的解釋一下, 在recyclerView上套一層和itemView 上一樣的h...
- 之前的項目中有需要開發(fā)一個懸浮置頂?shù)男Ч?,今天看到一篇文章,也是講到這么一個效果,但是實現(xiàn)方法不同,明顯比自己的更...
- 利用RecyclerView.ItemDecoration實現(xiàn)的懸浮效果 getItemOffsets:通過Rec...
- 上圖: 本文代碼的Github地址 思路: 每一個RecyclerView的item的布局(下文叫itemUI...
- 12月5日下午,機關(guān)七支部全體黨員及入黨積極分子參加了以“憲法宣傳活動”為主題的主題黨日活動。此次活動特邀...