zoukankan      html  css  js  c++  java
  • RecycleView弹性滑动

    还有点bug,建议使用 LinearSnapHelper

      rvPilotList.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
    
    //                offsetY += dy;
    //
    //                int first = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
    //                int last = linearLayoutManager.findLastCompletelyVisibleItemPosition();
    //                View firstview = linearLayoutManager.findViewByPosition(first);
    //                if (viewWidth == 0) {
    //                    viewWidth = firstview.getHeight();
    //                }
    //                int offseta = firstview.getTop();
    //                float sx = 1f + (float) offseta / viewWidth;
    //                if (offsetY == 0) {
    //                    View view = linearLayoutManager.findViewByPosition(first + 1);
    //                    scaleUp(view);
    //                }
    //
    //                firstview.setScaleX(sx);
    //                View lastview = linearLayoutManager.findViewByPosition(last);
    //                offseta = recyclerView.getHeight() - lastview.getBottom();
    //                sx = 1f + (float) offseta / viewWidth;
    //                lastview.setScaleX(sx);
    
                }
    
                @Override
                public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                    if (newState == 0) {
                        int visibleItem = linearLayoutManager.getChildCount();
                        int coun = 0;
                        if (visibleItem % 2 == 1) {
                            coun = (visibleItem - 1) / 2;
                        } else {
                            coun = visibleItem / 2;
                        }
                        Log.i(TAG, "onScrollStateChanged: " + visibleItem);
                        Log.i(TAG, "onScrollStateChanged: coun" + coun);
                        int postion = linearLayoutManager.findFirstVisibleItemPosition();
                        Log.i(TAG, "onScrollStateChanged: postion" + postion);
                        View view;
                        Log.i(TAG, "findFirstCompletelyVisibleItemPosition: " + linearLayoutManager.findFirstCompletelyVisibleItemPosition());
                        if (postion == 0 && linearLayoutManager.findFirstCompletelyVisibleItemPosition() != 0) {
                            view = linearLayoutManager.findViewByPosition(0);
                        } else {
                            view = linearLayoutManager.findViewByPosition(postion + coun);
                        }
                        int pm = ((ViewGroup) view.getParent()).getMeasuredWidth();
                        int left = view.getLeft() + view.getWidth() / 2;
    
                        int offset = 0;
                        if (left == pm / 2) {
                            return;
                        }
                        if (left < pm / 2) {
                            offset = -(pm / 2 - left);
                        } else {
                            offset = left - pm / 2;
                        }
                        rvPilotList.smoothScrollBy(offset, 0);
                    }
                }
            });
    

      

  • 相关阅读:
    [异常处理]class kafka.common.UnknownTopicOrPartitionException (kafka.server.ReplicaFetcherThread)
    ceph安装问题
    “云赞奖”投票结果出炉!快来看看你和你的心中所属是否获奖了!
    Azure 5 月新公布(二)
    云计算安全合规认证哪家强?
    少侠,找个千手观音来帮你营销可好?
    云应用也可以像搭积木一样搭出来你造吗?
    Azure 5 月新公布
    Azure本月最新活动,速度Mark!!
    Azure 进阶攻略 | 上云后的系统,「门禁」制度又该如何实现?
  • 原文地址:https://www.cnblogs.com/6324/p/10733971.html
Copyright © 2011-2022 走看看