美文网首页
recyclerview 弹性滑动 + 中间元素放大

recyclerview 弹性滑动 + 中间元素放大

作者: 卜卜Bruce | 来源:发表于2018-04-24 23:24 被阅读0次
    import android.animation.ValueAnimator;
    import android.content.Context;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.view.View;
    
    /**
     * Created by Bruce .
     */
    
    public class TempRecyclerView extends RecyclerView {
        public TempRecyclerView(@NonNull Context context) {
            this(context,null);
        }
    
        public TempRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        private void init() {
    
        }
        LinearLayoutManager linearLayoutManager;
        @Override
        public void setLayoutManager(LayoutManager layout) {
            super.setLayoutManager(layout);
            linearLayoutManager = (LinearLayoutManager) layout;
        }
    
        int viewHeight = 0;
        @Override
        public void onScrollStateChanged(int state) {
            Log.d("bruce","state:"+state);
            super.onScrollStateChanged(state);
            if(state == 0){
                int postion = linearLayoutManager.findFirstVisibleItemPosition();
                View view = linearLayoutManager.findViewByPosition(postion);
                int top = view.getTop();
                int offset = 0;
                if(viewHeight == 0){
                    viewHeight = view.getHeight();
                }
                if(top == 0){
                    return;
                }
                else if(-top < viewHeight/2){
                    offset = top;
                }
                else {
                    offset = viewHeight+top;
                }
                smoothScrollBy(0, offset);
            }
        }
    
        int offsetY = 0;
    
    
        @Override
        public void onScrolled(int dx, int dy) {
            super.onScrolled(dx, dy);
            offsetY+=dy;
    
            int first = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
            int last = linearLayoutManager.findLastCompletelyVisibleItemPosition();
            View firstview = linearLayoutManager.findViewByPosition(first);
            if(viewHeight == 0){
                viewHeight = firstview.getHeight();
            }
                int offseta = firstview.getTop();
            float sx = 1f+(float) offseta/viewHeight;
            if(offsetY == 0){
                View view = linearLayoutManager.findViewByPosition(first+1);
                view.setScaleX(2);
            }
    
            firstview.setScaleX(sx);
            View lastview = linearLayoutManager.findViewByPosition(last);
            offseta = getHeight()-lastview.getBottom();
            sx = 1f+(float) offseta/viewHeight;
            lastview.setScaleX(sx);
        }
    
    
    }
    

    相关文章

      网友评论

          本文标题:recyclerview 弹性滑动 + 中间元素放大

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