写此文章主要用于笔记,因为本人忘性比较大,实现过的东西大多记不住,之后又会费时重新实现,现决定养成好习惯,把不常实现的功能,以及不常用的知识点都记录下来。
第一篇要记录一下Recyclerview.ItemDecoration功能(个人理解为子类修饰功能,各位看官有专业术语的,就当我抛砖引玉了)。
写在前面——用法很简单,创建类继承RecyclerView.ItemDecoration,对布局重新设置之后通过RecyclverView的addItemDecoration(Context context)方法即可。
实现图如下
解释一下:1.getItemOffsets()
本次实现时间轴主要方法,对于它的作用,解释的通俗点可以理解为子布局的推移效果(RecyclerView是继承ViewGroup的所以我说子布局)
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
//重中之重,时间轴实现的核心,可以理解为,在某一个方向将子布局推移
//参数含义:相对于父布局左偏移,上偏移,右偏移,下偏移 (个人理解:变相margin值)
outRect.set(itemView_leftinterval,0,0,0);
}
2.OnDraw()
这个就很简单了,不解释。
在此感谢Carson_Ho大神提供的学习资料。下面贴出全部代码,如有需要的小伙伴拿走不谢,对于代码质量有更好建议的,欢迎提出来,共同进步。
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import com.example.myapplication.R;
/**
* Create By MR.D
* 我珍惜一眼而过的青春,才如此疯狂的面对未来。
* 2019/5/30
* USE: Recyclerview时间轴(2)
**/
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
private int circle_radius;
private Paint paint;
private int itemView_leftinterval;
private int icon_width;
private Bitmap bitmap;
private Context context;
public DividerItemDecoration(Context context) {
this.context = context;
paint = new Paint();
//画笔颜色:#FFD7D7D7
paint.setColor(context.getResources().getColor(R.color.gray));
//设置画笔宽度,可用于修改轴的宽度
paint.setStrokeWidth(dip2px(context,2));
//获取图片
bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.time_icon);
//矩形左侧偏移量px
itemView_leftinterval = 150;
//最后一个圆点的半径
circle_radius = dip2px(context,5);
//图标所在正方形宽高(一半)
icon_width = dip2px(context,8);
}
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
//重中之重,时间轴实现的核心,可以理解为,在某一个方向将子布局推移
//参数含义:相对于父布局左偏移,上偏移,右偏移,下偏移 (个人理解:变相margin值)
outRect.set(itemView_leftinterval,0,0,0);
}
@Override
public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.onDraw(c, parent, state);
c.drawColor(context.getResources().getColor(R.color.white));
int childCount = parent.getChildCount();
//遍历子布局,为每一个子布局进行绘制
for(int i = 0;i<childCount;i++){
View child = parent.getChildAt(i);
//下面两个参数是计算我们的矩形相对于父布局所在位置
int centerx = child.getLeft() - itemView_leftinterval / 2;
int centery = child.getTop() + child.getHeight() / 2;
//计算图标展示的正方形大小(相对于父布局位置)
Rect rect = new Rect(centerx-icon_width, centery-icon_width
, centerx+icon_width, centery+icon_width);
/**
* 上半轴绘制
*/
//上端点坐标(X,Y)
float upLine_up_x = centerx;
float upLine_up_y = child.getTop();
//下端点坐标(X,Y)
float upLine_bottom_x = centerx;
float upLine_bottom_y = centery - icon_width;
/**
* 如果是最后一个item绘制上半轴需要加长
*/
//上端点坐标(X,Y)
float upLine_last_up_x = centerx;
float upLine_last_up_y = child.getTop();
//下端点坐标(X,Y)
float upLine_last_bottom_x = centerx;
float upLine_last_bottom_y = centery - circle_radius;
/**
* 下半轴绘制
*/
//上端点(X,Y)
float bottomLine_up_x = centerx;
float bottom_up_y = centery + icon_width;
//下端点(X,Y)
float bottomLine_bottom_x = centerx;
float bottomLine_bottom_y = child.getBottom();
//顶部不绘制上端线,底部不绘制下端线且改变图标为实心圆
int index = parent.getChildAdapterPosition(child);
if(index == 0){
c.drawBitmap(bitmap,null,rect,paint);
c.drawLine(bottomLine_up_x,bottom_up_y,bottomLine_bottom_x,bottomLine_bottom_y,paint);
}else if(index + 1 == childCount){
c.drawCircle(centerx,centery,circle_radius,paint);
c.drawLine(upLine_last_up_x,upLine_last_up_y,upLine_last_bottom_x,upLine_last_bottom_y,paint);
}else{
c.drawBitmap(bitmap,null,rect,paint);
c.drawLine(upLine_up_x,upLine_up_y,upLine_bottom_x,upLine_bottom_y,paint);
c.drawLine(bottomLine_up_x,bottom_up_y,bottomLine_bottom_x,bottomLine_bottom_y,paint);
}
}
}
/**
* dp->px
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
}
适配器代码如下
RecyclerView timeRc = findViewById(R.id.timeRc);
timeRc.setLayoutManager(new LinearLayoutManager(TimeDamoActivity.this));
timeRc.setHasFixedSize(true);
timeRc.addItemDecoration(new DividerItemDecoration(this));
timeRc.setAdapter(new TimeRcAdapter(R.layout.recyclerview_list_layout,list));