package com.adealink.weparty.module.room.chat.livemessage;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;

import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView;

public class AdjustLinearSmoothScroller extends LinearSmoothScroller {

    public float duration = 300f; //越小越快

    private final int mOffsetPx;

    public AdjustLinearSmoothScroller(Context context, int offsetPx) {
        super(context);
        mOffsetPx = offsetPx;
    }

    @Override
    protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
        return duration / displayMetrics.densityDpi;
    }

    @Override
    protected void onTargetFound(View targetView, RecyclerView.State state, Action action) {
        int dy = calculateDyToMakeVisible(targetView, getVerticalSnapPreference());
        dy -= mOffsetPx; // 可能不精确, 加一个偏移量
        final int time = calculateTimeForDeceleration(dy);
        if (time > 0) {
            action.update(0, -dy, time, mDecelerateInterpolator);
        }
    }

    public void setDuration(float milliseconds) {
        duration = milliseconds;
    }

}
