Android:控制在回收站视图上平滑滚动 [英] Android : Control Smooth scroll over recycler view
问题描述
我正在使用带有 CardView 的 Recyclerview.我知道如何控制列表视图上的速度.但不适用于 Recyclerview.
I am using Recyclerview with CardView. I am aware how to control speed on list view. But not for Recyclerview.
我在找到的类名中搜索了很多SmoothScroll.怎么用那个?我不知道!现在 Recyclerview 默认滚动速度很快.
I searched a lot in found class name SmoothScroll. How to use that? I have no Idea! Right now Recyclerview by default scroll is fast.
更新:
我用 this
推荐答案
当您说smoothScroll
"时,您不清楚您的意思.您可能指的是自动smoothScrollToPosition
",它会自动滚动到指定位置,您可能指的是手动滚动,也可能指的是投掷.为了繁荣,我现在将尝试回答所有这些问题.
It's unclear what you mean when you say "smoothScroll
". You could be referring to the automatic "smoothScrollToPosition
" which will automatically scroll to a specified position, you could be talking about manual scrolling and you could be talking about flinging. For the sake of prosperity, I will attempt to answer all of these issues now.
1.自动平滑滚动.
在你的布局管理器中,你需要实现 smoothScrollToPosition
方法:
Inside your layout manager, you need to implement the smoothScrollToPosition
method:
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, State state, int position)
{
// A good idea would be to create this instance in some initialization method, and just set the target position in this method.
LinearSmoothScroller smoothScroller = new LinearSmoothScroller(getContext())
{
@Override
public PointF computeScrollVectorForPosition(int targetPosition)
{
int yDelta = calculateCurrentDistanceToPosition(targetPosition);
return new PointF(0, yDelta);
}
// This is the important method. This code will return the amount of time it takes to scroll 1 pixel.
// This code will request X milliseconds for every Y DP units.
@Override
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics)
{
return X / TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, Y, displayMetrics);
}
};
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
在这个例子中,我使用了一个名为calculateCurrentDistanceToPosition"的辅助方法.这可能有点棘手,因为它涉及跟踪您当前的滚动位置,并计算给定 y 位置的滚动位置.您可以在 此处 找到有关如何跟踪回收者滚动的示例.一>.
In this example, I use a helper method named "calculateCurrentDistanceToPosition". This can be a bit tricky, since it involves keeping track of your current scroll position, and calculating the scroll position of a given y position. You can find an example of how to keep track of the recycler's scroll y here.
计算给定位置的滚动 y 实际上取决于您的回收站显示的内容.假设您所有的物品高度相同,您可以通过执行以下计算来计算:
Calculating the scroll y of a given position is really dependent on what your recycler is displaying. Assuming all your items are the same height, you can calculate this by performing the following calculation:
targetScrollY = targetPosition * itemHeight
然后,要计算您需要滚动的距离,只需将当前滚动 y 与目标滚动 y 相减即可:
Then, to calculate the distance you need to scroll, simply subtract the current scroll y with the target scroll y:
private int calculateCurrentDistanceToPosition(int targetPosition) {
int targetScrollY = targetPosition * itemHeight;
return targetScrollY - currentScrollY;
}
2.降低手动滚动速度.
再次,您需要编辑您的布局管理器,这一次 - scrollVerticallyBy
方法:
Once again, you need to edit your layout manager, this time - the scrollVerticallyBy
method:
@Override
public int scrollVerticallyBy(int delta, Recycler recycler, State state)
{
// write your limiting logic here to prevent the delta from exceeding the limits of your list.
int prevDelta = delta;
if (getScrollState() == SCROLL_STATE_DRAGGING)
delta = (int)(delta > 0 ? Math.max(delta * MANUAL_SCROLL_SLOW_RATIO, 1) : Math.min(delta * MANUAL_SCROLL_SLOW_RATIO, -1));
// MANUAL_SCROLL_SLOW_RATIO is between 0 (no manual scrolling) to 1 (normal speed) or more (faster speed).
// write your scrolling logic code here whereby you move each view by the given delta
if (getScrollState() == SCROLL_STATE_DRAGGING)
delta = prevDelta;
return delta;
}
在上面的方法中,我调用了getScrollState()
".这是RecyclerView
的一个方法.在这个实现中,我的自定义 LayoutManager
是我的自定义 RecyclerView
的嵌套类.如果这对您不起作用,您可以尝试通过某种界面模式获取滚动状态.
In the above method, I call "getScrollState()
". This is a method of RecyclerView
. In this implementation, my custom LayoutManager
is a nested class of my custom RecyclerView
. If this doesn't work for you, you can try to grab the scroll state via some interface pattern.
3.放慢投掷速度
在这里,您要按比例降低投掷速度.您将需要覆盖 RecyclerView 子类中的 fling
方法:
Here you want to scale down the fling velocity. You will need to override the fling
method inside your RecyclerView subclass:
@Override
public boolean fling(int velocityX, int velocityY)
{
velocityY *= FLING_SCALE_DOWN_FACTOR; // (between 0 for no fling, and 1 for normal fling, or more for faster fling).
return super.fling(velocityX, velocityY);
}
我很难提供更量身定制的解决方案,因为您没有发布任何代码,也没有提供有关您的设置的大量信息,但我希望这涵盖了大多数基础,并能帮助您找到最适合您的解决方案.
It's difficult for me to provide a more tailored solution, since you didn't post any of your code, or provide much information about your setup, but I hope this covers most bases and will help you find the best solution for you.
这篇关于Android:控制在回收站视图上平滑滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!