不会自定义View的厨子,写不出好代码!
首先,说说需求:
1.列表自定义分组;
2.分组类标标题实现吸顶效果;
先上效果图:
其实就是自定义一个 RecyclerView.ItemDecoration,自己实现分割线,分组标签也是分割线来实现的:上代码
package com.example.recylerviewdemo;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextPaint;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
/**
*
*/
class StarItemDecrationextends RecyclerView.ItemDecoration {
private int HEAD_HEIGHT ;
private PaintmPaint;
private PainttextPaint;
private RecttextRect;
public StarItemDecration(Context context) {
HEAD_HEIGHT = dp2px(context,120);
mPaint =new Paint();
mPaint.setColor(Color.RED);
textPaint =new TextPaint();
textPaint.setTextSize(50);
textPaint.setColor(Color.BLACK);
textRect =new Rect();
}
@Override
public void onDraw(@NonNull Canvas canvas, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.onDraw(canvas, parent, state);
if(parent.getAdapter()instanceof StartAdapter) {
StartAdapter startAdapter = (StartAdapter) parent.getAdapter();
int childCount = parent.getChildCount();
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
for (int i =0; i < childCount; i++) {
View view = parent.getChildAt(i);
int positon = parent.getChildLayoutPosition(view);
boolean isHead = startAdapter.isGroupHeader(positon);
if(isHead && view.getTop() -HEAD_HEIGHT -parent.getPaddingTop() >=0) {
canvas.drawRect(left,view.getTop() -HEAD_HEIGHT,right,view.getTop(),mPaint);
String groupName = startAdapter.getGroupName(positon);
textPaint.getTextBounds(groupName,0,groupName.length(),textRect);
canvas.drawText(groupName,left +20,view.getTop() -HEAD_HEIGHT/2 +textRect.height()/2,textPaint);
}else if(view.getTop() -HEAD_HEIGHT -parent.getPaddingTop() >=0){
canvas.drawRect(left,view.getTop() -3,right,view.getTop(),mPaint);
}
}
}
}
@Override
public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.onDrawOver(c, parent, state);
if(parent.getAdapter()instanceof StartAdapter) {
StartAdapter startAdapter = (StartAdapter) parent.getAdapter();
int position = ((LinearLayoutManager) parent.getLayoutManager()).findFirstVisibleItemPosition();
View itemView = parent.findViewHolderForAdapterPosition(position).itemView;
int left = parent.getLeft();
int right = parent.getWidth() - parent.getPaddingRight();
int top = parent.getPaddingTop();
String groupName = startAdapter.getGroupName(position );
if(startAdapter.isGroupHeader(position +1)){
int bottom = Math.min(HEAD_HEIGHT,itemView.getBottom() - top);
c.drawRect(left,top,right,top + bottom,mPaint);
textPaint.getTextBounds(groupName,0,groupName.length(),textRect);
c.drawText(groupName,left +20,itemView.getTop() +HEAD_HEIGHT/2 +textRect.height()/2,textPaint);
}else{
c.drawRect(left,top,right,top +HEAD_HEIGHT,mPaint);
textPaint.getTextBounds(groupName,0,groupName.length(),textRect);
c.drawText(groupName,left +20,top +HEAD_HEIGHT/2 +textRect.height()/2,textPaint);
}
}
}
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
if(parent.getAdapter()instanceof StartAdapter) {
StartAdapter startAdapter = (StartAdapter) parent.getAdapter();
int positon = parent.getChildLayoutPosition(view);
if(startAdapter.isGroupHeader(positon)){
outRect.set(0, HEAD_HEIGHT, 0, 0);
}else{
outRect.set(0, 3, 0, 0);
}
}
}
private int dp2px(Context context, float dp){
float scale = context.getResources().getDisplayMetrics().density;
return (int) (dp * scale *0.5f);
}
}