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的itemcount
public int getItemCount() {    final Adapter a = mRecyclerView != null ? mRecyclerView.getAdapter() : null;    return a != null ? a.getItemCount() : 0;}
到此,这个问题解决。