我正在将Recyclerview与CardView一起使用我知道如何在列表视图上控制速度。但对于RecyclerView不是。
更新:
我用这个总结了吉尔的回答
不清楚您说“SmoothScroll
”是什么意思。您可以引用自动的“SmoothScrollToposition
”,它将自动滚动到指定的位置,您可以讨论手动滚动,也可以讨论抛出。为了繁荣,我现在将尝试回答所有这些问题。
1.自动平滑滚动。
在布局管理器中,需要实现SmoothScrollToposition
方法:
@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);
}
计算给定位置的滚动y实际上取决于回收器显示的内容。假设您的所有项目都是相同的高度,您可以通过执行以下计算来计算这一点:
targetScrollY = targetPosition * itemHeight
然后,要计算你需要滚动的距离,只需用目标滚动y减去当前滚动y即可:
private int calculateCurrentDistanceToPosition(int targetPosition) {
int targetScrollY = targetPosition * itemHeight;
return targetScrollY - currentScrollY;
}
2.减缓手动滚动。
@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;
}
在这里,你想要缩小投掷速度。您需要重写RecyclerView子类中的fling
方法:
@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);
}
我很难提供一个更定制的解决方案,因为您没有发布任何代码,也没有提供关于您的设置的很多信息,但我希望这涵盖了大多数基础,并将帮助您找到最适合您的解决方案。
我做了一个水平循环视图,效果很好(多亏了这个),但滚动和数据的方向是从左向右扩展的;那么,我该如何更改RecyclerView滚动方向,如下图所示? 我的代码:
在其他回收器视图中有一个回收器视图。两者都需要垂直滚动。外部回收器视图滚动正常,但内部回收器视图滚动不正常。 这是代码: ViewAdapter如下: 我尝试了以下两种回收商观点,但都无法解决问题 也尝试了这个:
我在ViewPager中的NestedScrollView中有一个水平的RecyclerView。现在当我尝试滚动RecyclerView时,有时它滚动,但有时只有ViewPager滚动。 这就是我的RecyclerView XML的样子: 到目前为止,我所尝试的,正如你所看到的,我写了一个自定义的viewpager。我尝试告诉ViewPager,如果滚动来自RecyclerView,它就不能滚动
我想做的从右到左滚动与我猜将是一个recylcer视图,但不确定是否是这个。我只是需要有人给我指明正确的方向。我想我需要一个内部布局的回收视图。 如果有人能指点我在哪里找到答案,我会变得很伟大!
我有一个水平回收视图。每个子级包含一个TextView和一个垂直RecyclerView。垂直RecyclerView的子级仅包含TextView。 垂直滚动非常平滑,但水平滚动滞后很多。我已经尝试在onBindViewHolder中使用swapAdapter,而不是setAdapter,但这并没有解决问题。我也尝试过更改数据集并调用notifyDataSetChanged(),但这也没有帮助。
我有 主视图 在我的模板适配器上,我有另一个用于子视图的回收器适配器 代码工作,但是当用户在android设备上滚动时,父scrollView不允许我滚动回收器视图(1)