本篇文章给大家分享的是有关使用RecyclerView时如何去除底部的分割线,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
自定义分割线
通过分析源码后我们发现没有相关的方法来处理这一需求,所以只能自定义分割线,通过上一篇文章的基础我们知道肯定是修改getItemOffsets。那我们的思路是让这个方法在到最后一个条目时,不偏移分割线的间隙。同时也要修改onDraw方法,让他不再绘制最后一条分割线。
private void drawHorizontal(Canvas c, RecyclerView parent, State state) {
int childCount = parent.getChildCount() - 1;
int left = 0;
int top = parent.getPaddingTop();
int right = 0;
int bottom = parent.getHeight() - parent.getPaddingBottom();
for (int i = 0; i < childCount; i++) {
View view = parent.getChildAt(i);
RecyclerView.LayoutParams params = (LayoutParams) view.getLayoutParams(); //考虑,padding
left = view.getRight();
right = left + divider.getIntrinsicHeight(); //我们在自定义drawable的是是,写死了高度,所以只能用高度
divider.setBounds(left, top, right, bottom);
divider.draw(c);
}
}
/**
* 思路:就是获取每个item,计算divider的left,top,right,bottom
*/
private void drawVertical(Canvas c, RecyclerView parent, State state) {
int childCount = parent.getChildCount() - 1;
int left = parent.getPaddingLeft();
int top = 0;
int right = parent.getWidth() - parent.getPaddingRight(); //考虑右边的padding
int bottom = 0;
for (int i = 0; i < childCount; i++) { //不绘制最后一个条目的分割线
View view = parent.getChildAt(i);
RecyclerView.LayoutParams params = (LayoutParams) view.getLayoutParams(); //考虑,padding
top = view.getBottom() + params.topMargin; //就是当前view底部到顶部的距离
bottom = top + divider.getIntrinsicHeight() - params.bottomMargin; //就是top+divider高度
divider.setBounds(left, top, right, bottom);
divider.draw(c);
}
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {
int childAdapterPosition = parent.getChildAdapterPosition(view);
int lastCount = parent.getAdapter().getItemCount() - 1;
//如果当前条目与是最后一个条目,就不设置divider padding
if (childAdapterPosition == lastCount) {
outRect.set(0, 0, 0, 0);
return;
}
Log.d("TAG", childAdapterPosition + "," + lastCount);
if (orientation == LinearLayoutCompat.HORIZONTAL) {
outRect.set(0, 0, divider.getIntrinsicHeight(), 0); //0,0,30,0,设置宽度
} else {
outRect.set(0, 0, 0, divider.getIntrinsicHeight()); //0,0,0,30,设置高度
}
}
以上就是使用RecyclerView时如何去除底部的分割线,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。