Recyclerview实现滑动放大ItemView

时间:2025-01-26 07:34:43

2.1实现方式

  1. 横向RecyclerView,因为recyclerview可以实现数据的更多加载,所以当position为最后一个的时候,可以重新请求第二页的数据,进行刷新;
  2. 后期也可以在此基础上添加Viewpager实现动态联动;

2.2核心代码

  1. 解决滑动时候,Item的动态的位置校准,对齐下方的指示图标;
        public static CenterViewItem getMinDifferItem(List<CenterViewItem> itemHeights){
            CenterViewItem minItem = (0); //默认第一个是最小差值
            for (int i = 0; i < (); i++) {
                //遍历获取最小差值
                if ((i).differ <= ){
                    minItem = (i);
                }
            }
            return minItem;
        }
  2. 解决滑动时候,流畅的大小变化,重写LinearLayoutManager;
      @Override
        public int scrollVerticallyBy(int dy,  recycler,  state) {
            int orientation = getOrientation();
            if (orientation == VERTICAL) {
                int scrolled = (dy, recycler, state);
                float midpoint = getHeight() / ;
                float d0 = ;
                float d1 = mShrinkDistance * midpoint;
                float s0 = ;
                float s1 =  - mShrinkAmount;
                for (int i = 0; i < getChildCount(); i++) {
                    View child = getChildAt(i);
                    float childMidpoint =
                            (getDecoratedBottom(child) + getDecoratedTop(child)) / ;
                    float d = (d1, (midpoint - childMidpoint));
                    float scale = s0 + (s1 - s0) * (d - d0) / (d1 - d0);
                    (scale);
                    (scale);
                }
                return scrolled;
            } else {
                return 0;
            }
        }
    
        @Override
        public int scrollHorizontallyBy(int dx,  recycler,  state) {
            int orientation = getOrientation();
            if (orientation == HORIZONTAL) {
                int scrolled = (dx, recycler, state);
    
                float midpoint = getWidth() / ;
                float d0 = ;
                float d1 = mShrinkDistance * midpoint;
                float s0 = ;
                float s1 =  - mShrinkAmount;
                for (int i = 0; i < getChildCount(); i++) {
                    View child = getChildAt(i);
                    float childMidpoint =
                            (getDecoratedRight(child) + getDecoratedLeft(child)) / ;
                    float d = (d1, (midpoint - childMidpoint));
                    float scale = s0 + (s1 - s0) * (d - d0) / (d1 - d0);
                    (scale);
                    (scale);
                }
                return scrolled;
            } else {
                return 0;
            }
    
        }