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

本文介绍了一种自定义RecyclerView的实现方式,通过继承RecyclerView并覆盖其onScrollStateChanged和onScrolled方法,实现了特殊的滚动效果。文章详细展示了如何通过计算当前可见视图的位置和高度来平滑滚动,并调整视图的缩放比例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
    }


}
复制代码

转载于:https://juejin.im/post/5c8754f5f265da2de52dc6dc

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值