RecyclerView获取item的count
来源:互联网 发布:白金网络加速器下载 编辑:程序博客网 时间:2024/05/21 17:23
一切都来自于今天下午碰到的一个分割线的问题。UI要求recyclerview横向滑动,并且第一项的左边距和最后一项的右边距一样同时大于其他的item的间距。代码随手就来:
写分割线,当然要继承ItemDecoration了,当然要重写两个方法了,一个onDraw,看名字也知道这个是用来绘制的,画布都准备好了放在方法的参数里,画笔得自己造,这让我突然想起来神笔马良。一个getItemOffsets,看名字,这个是用来确认item的decoratoin的offset的。清晰明了,开干。看我下面的代码:
@Overridepublic void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { super.onDraw(c, parent, state); Rect rect = new Rect(); rect.top = parent.getTop(); rect.bottom = parent.getBottom(); for (int i = 0; i < parent.getChildCount(); i++) { View child = parent.getChildAt(i); if (i==0){ rect.right = child.getRight(); rect.left = rect.right+dipToPx(context,15); c.drawRect(rect,paint); }else if(i==parent.getChildCount()-1) { rect.left = child.getLeft()+dipToPx(context,10); rect.right = child.getRight()+dipToPx(context,15); c.drawRect(rect,paint); }else { rect.right = child.getRight(); rect.left = rect.right+dipToPx(context,10); c.drawRect(rect,paint); } }}@Overridepublic void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { super.getItemOffsets(outRect, view, parent, state); int position = parent.getChildAdapterPosition(view); if (position==0){ outRect.left = dipToPx(context,15); }else if(position==parent.getChildCount()-1){ outRect.left = dipToPx(context,10); outRect.right = dipToPx(context,15); }else{ outRect.left = dipToPx(context,10); }}
就是这么简单随意,分割线分分钟画好,而且是按照UI要求的画的。shift+f10,运行着,下去抽根烟。
但是,事情似乎并没有朝着预想的方向发展。回来一看,what's the bi bi bi...怎么回事,为什么应该出现在最后一项最右边的分割线出现在了第三个item的右边?
果断断点调试,断点条件是当position=9时,然后发现无法获取这个条件,同时,返现parent.getchiilcount()返回的是3,但是我的数据源明明是10个啊?看来
是发现问题了,果断把我知道的关于获取个数的方法全部打印一下。于是有了下面的代码:
@Overridepublic void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { super.onDraw(c, parent, state); Rect rect = new Rect(); rect.top = parent.getTop(); rect.bottom = parent.getBottom(); for (int i = 0; i < parent.getChildCount(); i++) { View child = parent.getChildAt(i); if (i==0){ rect.right = child.getRight(); rect.left = rect.right+dipToPx(context,15); c.drawRect(rect,paint); }else if(i==parent.getAdapter().getItemCount()-1) { rect.left = child.getLeft()+dipToPx(context,10); rect.right = child.getRight()+dipToPx(context,15); c.drawRect(rect,paint); }else { rect.right = child.getRight(); rect.left = rect.right+dipToPx(context,10); c.drawRect(rect,paint); } }}@Overridepublic void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { super.getItemOffsets(outRect, view, parent, state); int position = parent.getChildAdapterPosition(view); if (position==0){ outRect.left = dipToPx(context,15); }else if(position==parent.getLayoutManager().getItemCount()-1){ outRect.left = dipToPx(context,10); outRect.right = dipToPx(context,15); }else{ outRect.left = dipToPx(context,10); } int i = parent.getLayoutManager().getChildCount();//这个获取可见的数量 int j = parent.getLayoutManager().getItemCount();//这个可以获取所有的数量 int h = parent.getChildCount();//这个获取可见的数量 int g = parent.getAdapter().getItemCount();//这个可以获取所有的数量 Log.e("long",i+"=="+j+"=="+h+"=="+g+"=="+position);}结论我已经写在注释里了。然后改动后的代码就是这样子。
看来recyclerview还是跟listview不太一样啊。追源码发现:
parent.getLayoutManager().getChildCount是取的mChildHelper.getChildCount().
mCallback.getChildCount() - mHiddenViews.size();,这个是获取所有的count减去隐藏的个数。getItemCount是获取adapter的itemcountpublic int getItemCount() { final Adapter a = mRecyclerView != null ? mRecyclerView.getAdapter() : null; return a != null ? a.getItemCount() : 0;}到此,这个问题解决。
阅读全文
0 0
- RecyclerView获取item的count
- RecyclerView 获取 item 的位置
- RecyclerView 获取item,删除item的坑
- RecyclerView 获取显示item的位置
- 如何获取recyclerView的第一个Item的视图
- RecyclerView 获取滑动停止时展示出的item范围
- RecyclerView的item监听
- RecyclerView的Item监听
- TV中RecyclerView添加item的点击事件和删除item之后获取焦点解决
- RecyclerView 的 item 的间距
- RecyclerView Item 的点击事件
- RecyclerView的item点击事件
- recyclerview item的进入动画
- RecyclerView的item点击事件
- RecyclerView的item点击事件
- RecyclerView的Item点击事件
- 控制RecyclerView Item的宽度
- RecyclerView的item点击事件
- libxxx.so- text relocations问题的终极解决方案
- 吉信通短信接口(HTTP协议) java
- EasyUI DataGrid 鼠标悬停提示
- Kreas 训练自己的图像数据处理回归问题
- 文章标题
- RecyclerView获取item的count
- springMVC原理解析及常用注解
- 2017微信数据报告,9亿流量,家电售后服务如何渗透
- Spring boot 中使用 Thymeleaf
- jqgrid自动换行
- useradd in linux
- linux 配置nfs挂载共享目录
- C#WinForm程序异常退出的捕获、继续执行与自动重启
- pulltorefresh(上拉下拉加载数据) 使用方法!