gpt4 book ai didi

android - Recyclerview 使用 scrollBy 方法控制滚动速度

转载 作者:行者123 更新时间:2023-11-29 02:27:43 24 4
gpt4 key购买 nike

亲爱的,我已经设法通过自定义线性布局管理器使用 scrollToPosition 方法控制滚动速度。

但是我使用了scrollBy(int dx, int dy)方法,滚动速度仍然是recyclerview speed的默认滚动速度。

知道如何更改此特定方法的滚动速度吗?

这是我的布局代码:

    package com.arabiaweather.maater.screens.livesatellite.components

import android.content.Context
import android.graphics.PointF
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.LinearSmoothScroller
import android.support.v7.widget.RecyclerView
import android.util.DisplayMetrics

class CustomLinearLayoutManager : LinearLayoutManager {

var millisPerInch = 250.0f

constructor(context: Context) : super(context) {}

constructor(context: Context, orientation: Int, reverseLayout: Boolean) : super(context, orientation, reverseLayout) {}

override fun smoothScrollToPosition(recyclerView: RecyclerView, state: RecyclerView.State?, position: Int) {

val linearSmoothScroller = object : LinearSmoothScroller(recyclerView.context) {

override fun computeScrollVectorForPosition(targetPosition: Int): PointF? {
return this@CustomLinearLayoutManager
.computeScrollVectorForPosition(targetPosition)
}

override fun calculateSpeedPerPixel(displayMetrics: DisplayMetrics): Float {
return millisPerInch / displayMetrics.densityDpi
}
}
linearSmoothScroller.targetPosition = position
startSmoothScroll(linearSmoothScroller)
}

override fun startSmoothScroll(smoothScroller: RecyclerView.SmoothScroller?) {
super.startSmoothScroll(smoothScroller)
}

override fun scrollToPositionWithOffset(position: Int, offset: Int) {
super.scrollToPositionWithOffset(position, offset)
}


}

最佳答案

我觉得没有办法。

查看 Android 代码,我看到 ViewFlinger 类包含一个允许按距离和速度设置滚动的方法:

class ViewFlinger implements Runnable {
     ...
...
public void smoothScrollBy(int dx, int dy, int vx, int vy) {
            smoothScrollBy(dx, dy, computeScrollDuration(dx, dy, vx, vy));
}
}

不幸的是,由于 RecyclerView.mViewFlinger 是私有(private)的,我看不到从 RecyclerView 访问此功能的方法。我希望有一种方法可以从继承类访问它。

另一种方法是扩展 ViewFlinger 类并覆盖函数 computeScrollDuration(),但我也没有看到这样做的方法(ViewFlinger 类在 RecyclerView 中是包私有(private)的)

        private int computeScrollDuration(int dx, int dy, int vx, int vy) {
final int absDx = Math.abs(dx);
final int absDy = Math.abs(dy);
final boolean horizontal = absDx > absDy;
final int velocity = (int) Math.sqrt(vx * vx + vy * vy);
final int delta = (int) Math.sqrt(dx * dx + dy * dy);
final int containerSize = horizontal ? getWidth() : getHeight();
final int halfContainerSize = containerSize / 2;
final float distanceRatio = Math.min(1.f, 1.f * delta / containerSize);
final float distance = halfContainerSize + halfContainerSize
* distanceInfluenceForSnapDuration(distanceRatio);

final int duration;
if (velocity > 0) {
duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
} else {
float absDelta = (float) (horizontal ? absDx : absDy);
duration = (int) (((absDelta / containerSize) + 1) * 300);
}
return Math.min(duration, MAX_SCROLL_DURATION);
}

理想情况下,RecyclerView.java 类会添加这些行以使其成为可能:

    public void smoothScrollBy(int dx, int dy, int vx, int vy, Interpolator interpolator) {
if (mLayout == null) {
Log.e(TAG, "Cannot smooth scroll without a LayoutManager set. "
+ "Call setLayoutManager with a non-null argument.");
return;
}
if (mLayoutFrozen) {
return;
}
if (!mLayout.canScrollHorizontally()) {
dx = 0;
}
if (!mLayout.canScrollVertically()) {
dy = 0;
}
if (dx != 0 || dy != 0) {
mViewFlinger.smoothScrollBy(dx, dy, vx, vy, interpolator);
}
}

所以我现在还没有找到办法。

关于android - Recyclerview 使用 scrollBy 方法控制滚动速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51281606/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com