美文网首页AndroidUI
使用ItemDecoration打造悬停分组头部以及加heade

使用ItemDecoration打造悬停分组头部以及加heade

作者: guoheng1996 | 来源:发表于2018-02-01 15:38 被阅读0次

    使用ItemDecoration打造悬停分组头部,网上很多,但是大部分头部都是一些文字图形啥的,不够灵活,于是我在此基础上使用了自定义view来显示分组头部和悬停view。

    先上图: Feb-01-2018 14-52-23.gif

    还有footer,视屏有限制,就不放上来了。

    可以看到页面上是一个recyclerView。可以分为以下这几部分,recyclerview,header, group(分组头部),以及悬停头部。悬停分组的知识已经很多人写了,我就不赘述了。 我主要说说这些自定义view是怎么画上去的。

    来看代码:

        @Override
        public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
            if (header != null) {
                final int childCount = parent.getChildCount();
                for (int i = 0; i < childCount; i++) {
    
                    View child = parent.getChildAt(i);
                    int position = parent.getChildAdapterPosition(child);
    
                    if (position == 0) {
                        c.translate(0, child.getTop() - header.getMeasuredHeight() - params.topMargin);
                        header.draw(c);
                        c.translate(0, header.getMeasuredHeight() - child.getTop() + params.topMargin);
                    }
                }
            }
        }
    

    这是给recyclerview加头部的代码。其中header需要我们提前给他处理好计算代码放在最后,这其中的关键代码即 c.translate(0, child.getTop() - header.getMeasuredHeight() - params.topMargin);
    设置绘制header的起点。绘制完后,把canvas还原,如不还原会导致recycler view本体绘制出问题(ondraw在recycler view之前绘制)。

      @Override
        public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
            if (group == null) {
                return;
            }
    
            int childCount = parent.getChildCount();
            for (int i = 0; i < childCount; i++) {
                View child = parent.getChildAt(i);
                int position = parent.getChildAdapterPosition(child);
                if (isGroupFirst(position)) {
                    ((BaseGroupRecyclerView) parent).setGroupData(group, position);
                    buildView(group);
                    int top = child.getBottom() - child.getHeight() - group.getMeasuredHeight() - params.topMargin;
                    c.translate(0, top);
                    group.draw(c);
                    c.translate(0, -top);
                }
            }
        }
    
        @Override
        public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
            super.onDrawOver(c, parent, state);
    
            if (!isStickHeader) {
                return;
            }
    
            final int childCount = parent.getChildCount();
            int preGroupName;
            int currentGroupName = -1;
            for (int i = 0; i < childCount; i++) {
    
                View view = parent.getChildAt(i);
                int position = parent.getChildAdapterPosition(view);
    
                preGroupName = currentGroupName;
                currentGroupName = mList.get(position);
                if (currentGroupName == preGroupName) {
                    continue;
                }
    
                int viewBottom = view.getBottom();
                int top = Math.max(group.getMeasuredHeight(), view.getTop());
    
                if (position + 1 < state.getItemCount()) {
                    int nextGroupName = mList.get(position + 1);
                    if (currentGroupName != nextGroupName && viewBottom + params.topMargin * 2 < top) {
                        top = viewBottom + params.topMargin * 2;
                    }
                }
    
                ((BaseGroupRecyclerView) parent).setGroupData(group, position);
                buildView(group);
                int marginTop = top - group.getMeasuredHeight() - params.topMargin;
                c.translate(0, marginTop);
                group.draw(c);
                c.translate(0, -marginTop);
                break;
            }
        }
    

    这段代码就是绘制分组头部(onDraw方法),在最上面悬停的头部(onDrawOver方法)。

    我们来读一下这段代码。先多group判空, 然后对屏幕上显示的item循环,看那个是这一分组的第一个,isGroupFirst这个方法自己定义呀,
    ((BaseGroupRecyclerView) parent).setGroupData(group, position);
    buildView(group);
    这两句是我用来根据位置信息改变group显示的内容,可以忽略。我是在自定义recycler view里面生成此用于分组的itemdecoration对象,所以我的recycler view里可以直接操作此group对象。

    buildView这个是因为更改了group的内容,重新测量一下group的宽高。

    接下来 计算group的起点,不熟悉的可以自己查查手机屏幕坐标系。
    然后group.draw,把group画出来。再还原c的起点。

    onDrawOver里的思想是当某一个分组头部到屏幕顶了, 就在recycler view上边画出此group,制造出悬浮的效果。具体逻辑就不看了。主要的关键就在canvas的位置的转换。

    写在最后,一开始我是看了这篇文章做的自定义view:
    http://blog.csdn.net/xyq046463/article/details/53009635?locationNum=3&fps=1

    他就做了一个头部,并且滑动的时候,把头部向上变换,我当时并不知道可以通过c.translate来改变画笔的起点位置来改变view显示的位置,于是我就把view.buildDrawingCache()变成一张图片,通过 :
    dst.set(left, top, right, bottom);设置图片的位置,
    c.drawBitmap(bitmap, null, dst, null);画图片。
    来调整view的位置,这种方法我leader说效率不好,view转换成bitmap耗时。于是我继续想办法,突然想到那个只做头部的老哥通过c.translate的方法来改变画笔的位置,于是我就想到了通过这种方法来改变view的显示方式。

    相关文章

      网友评论

        本文标题:使用ItemDecoration打造悬停分组头部以及加heade

        本文链接:https://www.haomeiwen.com/subject/ohmdzxtx.html