bootstrap 实现吸顶效果_RecyclerView超级简单的实现吸顶效果

不会自定义View的厨子,写不出好代码!

首先,说说需求:

1.列表自定义分组;

2.分组类标标题实现吸顶效果;

先上效果图:

ba196a60a701

ba196a60a701

ba196a60a701

其实就是自定义一个 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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值