2023年7月15日发(作者:)
RecyclerView底部分割线去除的⽅法概述之前我们抛出了⼀个问题就是RecyclerView在显⽰列表的时候,最底部也就是最后⼀个Item后⾯还有分割线,这⼤⼤影响界⾯的美观。这篇⽂章就是解决这个问题的。⾃定义分割线通过分析源码后我们发现没有相关的⽅法来处理这⼀需求,所以只能⾃定义分割线,通过上⼀篇⽂章的基础我们知道肯定是修改getItemOffsets。那我们的思路是让这个⽅法在到最后⼀个条⽬时,不偏移分割线的间隙。同时也要修改onDraw⽅法,让他不再绘制最后⼀条分割线。private void drawHorizontal(Canvas c, RecyclerView parent, State state) { int childCount = ldCount() - 1; int left = 0; int top = dingTop(); int right = 0; int bottom = ght() - dingBottom(); for (int i = 0; i < childCount; i++) { View view = ldAt(i); Params params = (LayoutParams) outParams(); //考虑,padding left = ht(); right = left + rinsicHeight(); //我们在⾃定义drawable的是是,写死了⾼度,所以只能⽤⾼度 nds(left, top, right, bottom); (c); }}/** * 思路:就是获取每个item,计算divider的left,top,right,bottom */private void drawVertical(Canvas c, RecyclerView parent, State state) { int childCount = ldCount() - 1; int left = dingLeft(); int top = 0; int right = th() - dingRight(); //考虑右边的padding int bottom = 0; for (int i = 0; i < childCount; i++) { //不绘制最后⼀个条⽬的分割线 View view = ldAt(i); Params params = (LayoutParams) outParams(); //考虑,padding top = tom() + gin; //就是当前view底部到顶部的距离 bottom = top + rinsicHeight() - Margin; //就是top+divider⾼度 nds(left, top, right, bottom); (c); }}@Overridepublic void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) { int childAdapterPosition = ldAdapterPosition(view); int lastCount = pter().getItemCount() - 1; //如果当前条⽬与是最后⼀个条⽬,就不设置divider padding if (childAdapterPosition == lastCount) { (0, 0, 0, 0); return; } Log.d("TAG", childAdapterPosition + "," + lastCount); if (orientation == NTAL) { (0, 0, rinsicHeight(), 0); //0,0,30,0,设置宽度 } else { (0, 0, 0, rinsicHeight()); //0,0,0,30,设置⾼度 }}使⽤这个⾃定义分割线的效果图如下:以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1689431268a247183.html
评论列表(0条)