recyclerview浮动item实现

recyclerview浮动item实现

recyclerview浮动item实现_第1张图片
GIF.gif

recyclerview浮动item实现_第2张图片
GIF.gif
  • 使用(支持两种模式)
  1. 指定浮动position可以多个,适用于多类型item的浮动
  2. 指定浮动viewtype,适用于单类型多item的浮动
container是用于显示浮动view的容器,我测试是直接加到recyclerview的framelayout父布局
#1
        floatInterface = new PositionFloatView(container, 11, 21, 36,38);
        floatInterface.attachRecyclerview(recyclerView1);
> 
#2
        floatInterface = new ViewTypeFloatView(container, 0);

  • 实现 通过监听recyclerview的滑动,调用adapter的creatviewholder 和bindviewholder产生需要浮动的view添加到根布局
 @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        int firstcompletevisable = getFirstcompletevisable(recyclerView);

        /**
         * {@link type=0 定了itemtype 1 定了position}
         * 启动子线程,一次计算10万内的type position 分段1000个记录一个标记位置用于优化查找
         * 超过计算位置,重启线程计算
         */
        int currentnearestposition = -1;
        if (type == 0) {
            return;
        } else {
            if (firstcompletevisable > arrays.get(arrays.size() - 1)) {
                /**
                 * 继续计算
                 */
               thread.start();
                type = 0;
                return;
            }
            int mark = arrays.size() - 1;
            int size = marks.size();
            for (int i = 0; i < size; i++) {
                if (marks.valueAt(i) > firstcompletevisable) {
                    mark = marks.keyAt(i);
                    break;
                }
            }
            for (int i1 = mark; i1 >= 0; i1--) {
                Integer integer = arrays.get(i1);
                if (integer < firstcompletevisable) {
                    currentnearestposition = integer;
                    break;
                }
            }
        }


        /**
         * 如果没有要悬浮的了,并且有悬浮过就隐藏掉
         */
        if (currentnearestposition == -1 && viewHolder != null) {
            viewHolder.itemView.setVisibility(View.GONE);
            currentfloatposition = -1;
            return;
        }


        /**
         * {@link crashmove}从一个悬浮view过渡到另一个的移动动画
         */
        if (crashmove && viewHolder != null) {
            View childAt = recyclerView.getChildAt(firstcompletevisable == getFirstvisable(recyclerView) ? 0 : 1);
            if (firstcompletevisable != -1 && adapter.getItemViewType(firstcompletevisable) == viewtype && childAt.getTop() <= viewHolder.itemView.getHeight()) {

                int height = viewHolder.itemView.getHeight();
                int top = height - childAt.getTop();
                if (top >= 0 && top <= height) {
                    viewHolder.itemView.setTranslationY(-top);
                }
            } else {
                viewHolder.itemView.setTranslationY(0);
            }
        }

        /**
         * 如果当前悬浮和需要悬浮的相等,不必做改动
         */
        if (currentnearestposition == currentfloatposition) {
            return;
        }

        if (currentfloatposition == firstcompletevisable && firstcompletevisable == getFirstvisable(recyclerView)) {
            return;
        }

        /**
         * 如果没有创建过,创建
         * 创建过,绑定新的数据
         */
        if (viewHolder == null) {
            viewHolder = adapter.createViewHolder(recyclerView, viewtype);
            container.addView(viewHolder.itemView);
        } else {
            viewHolder.itemView.setVisibility(View.VISIBLE);
        }
        currentfloatposition = currentnearestposition;
        if (listener != null) {
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onClick(v, currentfloatposition);
                }
            });
        }
        adapter.onBindViewHolder(viewHolder, currentfloatposition);
    }

    public int getFirstcompletevisable(RecyclerView recyclerView) {
        return RecyclerviewFloatHelper.getFirstcompletevisable(recyclerView);
    }

    public int getFirstvisable(RecyclerView recyclerView) {
        return RecyclerviewFloatHelper.getFirstvisable(recyclerView);
    }

    int MAX_PERIOD = 100000;
    SparseArray marks = new SparseArray<>();
    int mark = 0;

    @Override
    public void run() {
        System.out.println("所有type位置开始部分获取");
        int size = arrays.size();
        int i = 0;
        if (size > 0) {
            i = arrays.get(size - 1);
        }
        int end = MAX_PERIOD + i;

        for (; i < end && i < itemCount; i++) {
            if (adapter.getItemViewType(i) == viewtype) {
                arrays.add(i);
                if (i / 100 == mark) {
                    marks.put(arrays.size() - 1, i);
                    mark++;
                }
            }
        }
        System.out.println("共" + mark + "个标记位置");
        System.out.println("所有type位置获取完毕");
        type = 1;
    }

你可能感兴趣的:(recyclerview浮动item实现)