|
@@ -6,6 +6,7 @@ import android.content.Context; |
|
|
import android.graphics.Canvas; |
|
|
import android.graphics.Canvas; |
|
|
import android.graphics.Paint; |
|
|
import android.graphics.Paint; |
|
|
import android.graphics.Rect; |
|
|
import android.graphics.Rect; |
|
|
|
|
|
import android.util.Log; |
|
|
import android.view.View; |
|
|
import android.view.View; |
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull; |
|
|
import androidx.annotation.NonNull; |
|
@@ -22,7 +23,7 @@ public class TopItemDecoration extends RecyclerView.ItemDecoration{ |
|
|
|
|
|
|
|
|
public Context mContext; |
|
|
public Context mContext; |
|
|
//间隔高度 |
|
|
//间隔高度 |
|
|
private int mHeight = 60; |
|
|
|
|
|
|
|
|
private int mHeight = 50; |
|
|
//矩形画笔 |
|
|
//矩形画笔 |
|
|
private Paint mPaint=new Paint(); |
|
|
private Paint mPaint=new Paint(); |
|
|
//标签画笔 |
|
|
//标签画笔 |
|
@@ -46,20 +47,29 @@ public class TopItemDecoration extends RecyclerView.ItemDecoration{ |
|
|
int left = parent.getPaddingLeft(); |
|
|
int left = parent.getPaddingLeft(); |
|
|
int right =parent.getWidth() - parent.getPaddingLeft(); |
|
|
int right =parent.getWidth() - parent.getPaddingLeft(); |
|
|
int childCount = parent.getChildCount(); |
|
|
int childCount = parent.getChildCount(); |
|
|
for(int i=0;i<childCount;i++) |
|
|
|
|
|
|
|
|
try |
|
|
{ |
|
|
{ |
|
|
View childView = parent.getChildAt(i); |
|
|
|
|
|
int bottom = childView.getTop(); |
|
|
|
|
|
int top = bottom - mHeight; |
|
|
|
|
|
|
|
|
|
|
|
//绘制矩形间隔 |
|
|
|
|
|
c.drawRect(left, top, right, bottom, mPaint); |
|
|
|
|
|
//根据位置获取当前item的标签 |
|
|
|
|
|
String tag =typeListener.get(parent.getChildAdapterPosition(childView)); |
|
|
|
|
|
|
|
|
|
|
|
//绘制标签文本内容 |
|
|
|
|
|
textPaint.getTextBounds(tag, 0, tag.length(), mRound); |
|
|
|
|
|
c.drawText(tag, left + textPaint.getTextSize(), bottom - mHeight / 2 + mRound.height() / 2, textPaint); |
|
|
|
|
|
|
|
|
for(int i=0;i<childCount;i++) |
|
|
|
|
|
{ |
|
|
|
|
|
View childView = parent.getChildAt(i); |
|
|
|
|
|
int bottom = childView.getTop(); |
|
|
|
|
|
int top = bottom - mHeight; |
|
|
|
|
|
|
|
|
|
|
|
//绘制矩形间隔 |
|
|
|
|
|
c.drawRect(left, top, right, bottom, mPaint); |
|
|
|
|
|
//根据位置获取当前item的标签 |
|
|
|
|
|
int index= parent.getChildAdapterPosition(childView); |
|
|
|
|
|
if(index<typeListener.size()) |
|
|
|
|
|
{ |
|
|
|
|
|
String tag =typeListener.get(index); |
|
|
|
|
|
//绘制标签文本内容 |
|
|
|
|
|
textPaint.getTextBounds(tag, 0, tag.length(), mRound); |
|
|
|
|
|
c.drawText(tag, left + textPaint.getTextSize(), bottom - mHeight / 2 + mRound.height() / 2, textPaint); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
}catch (Exception ex) |
|
|
|
|
|
{ |
|
|
|
|
|
Log.e("异常", "onDraw: "+ex.getMessage()); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@@ -78,19 +88,19 @@ public class TopItemDecoration extends RecyclerView.ItemDecoration{ |
|
|
int right =parent.getWidth() - parent.getPaddingLeft(); |
|
|
int right =parent.getWidth() - parent.getPaddingLeft(); |
|
|
|
|
|
|
|
|
LinearLayoutManager manager = (LinearLayoutManager)parent.getLayoutManager(); |
|
|
LinearLayoutManager manager = (LinearLayoutManager)parent.getLayoutManager(); |
|
|
//第一个可见item位置 |
|
|
|
|
|
int index = manager.findFirstVisibleItemPosition(); |
|
|
|
|
|
if (index != -1) { |
|
|
|
|
|
//获取指定位置item的View信息 |
|
|
|
|
|
View childView = parent.findViewHolderForLayoutPosition(index).itemView; |
|
|
|
|
|
int top = parent.getPaddingTop(); |
|
|
|
|
|
String tag = typeListener.get(index); |
|
|
|
|
|
int bottom = parent.getPaddingTop() + mHeight; |
|
|
|
|
|
//悬浮置顶判断,其实也就是一直在绘制一个矩形加文本内容(上滑时取值bottom,下滑时取值childView.bottom.toFloat()) |
|
|
|
|
|
bottom = min(childView.getBottom(), bottom); |
|
|
|
|
|
c.drawRect(0f, top, right, bottom, mPaint); |
|
|
|
|
|
textPaint.getTextBounds(tag, 0, tag.length(), mRound); |
|
|
|
|
|
c.drawText(tag, left + textPaint.getTextSize(), bottom - mHeight / 2 + mRound.height() / 2, textPaint); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// //第一个可见item位置 |
|
|
|
|
|
// int index = manager.findFirstVisibleItemPosition(); |
|
|
|
|
|
// if (index != -1) { |
|
|
|
|
|
// //获取指定位置item的View信息 |
|
|
|
|
|
// View childView = parent.findViewHolderForLayoutPosition(index).itemView; |
|
|
|
|
|
// int top = parent.getPaddingTop(); |
|
|
|
|
|
// String tag = typeListener.get(index); |
|
|
|
|
|
// int bottom = parent.getPaddingTop() + mHeight; |
|
|
|
|
|
// //悬浮置顶判断,其实也就是一直在绘制一个矩形加文本内容(上滑时取值bottom,下滑时取值childView.bottom.toFloat()) |
|
|
|
|
|
// bottom = min(childView.getBottom(), bottom); |
|
|
|
|
|
// c.drawRect(0f, top, right, bottom, mPaint); |
|
|
|
|
|
// textPaint.getTextBounds(tag, 0, tag.length(), mRound); |
|
|
|
|
|
// c.drawText(tag, left + textPaint.getTextSize(), bottom - mHeight / 2 + mRound.height() / 2, textPaint); |
|
|
|
|
|
// } |
|
|
} |
|
|
} |
|
|
} |
|
|
} |