Sha*_*got 51 android android-support-library android-5.0-lollipop android-recyclerview
我正在使用Recyclerview和CardView.我知道如何控制列表视图的速度.但不适用于Recyclerview.
我在找到的类名SmoothScroll中搜索了很多.怎么用?我不知道!现在,Recyclerview默认滚动速度很快.
更新:
我总结吉尔回答与此
Gil*_*yof 103
当你说" 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);
}
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我使用名为"calculateCurrentDistanceToPosition"的辅助方法.这可能有点棘手,因为它涉及跟踪当前滚动位置,以及计算给定y位置的滚动位置.您可以在此处找到如何跟踪回收器滚动的示例.
计算给定位置的滚动y实际上取决于您的回收器显示的内容.假设您的所有项目都是相同的高度,您可以通过执行以下计算来计算:
targetScrollY = targetPosition * itemHeight
Run Code Online (Sandbox Code Playgroud)
然后,要计算您需要滚动的距离,只需使用目标滚动y减去当前滚动y:
private int calculateCurrentDistanceToPosition(int targetPosition) {
int targetScrollY = targetPosition * itemHeight;
return targetScrollY - currentScrollY;
}
Run Code Online (Sandbox Code Playgroud)
2.减慢手动滚动.
再次,您需要编辑布局管理器,这次 - scrollVerticallyBy方法:
@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;
}
Run Code Online (Sandbox Code Playgroud)
编辑:在上面的方法中,我调用" getScrollState()".这是一种方法RecyclerView.在此实现中,我的自定义LayoutManager是我的自定义的嵌套类RecyclerView.如果这对您不起作用,您可以尝试通过某种界面模式获取滚动状态.
3.减慢速度
在这里,您希望缩小投掷速度.您需要覆盖flingRecyclerView子类中的方法:
@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);
}
Run Code Online (Sandbox Code Playgroud)
由于您没有发布任何代码或提供有关您的设置的大量信息,因此我很难提供更具针对性的解决方案,但我希望这涵盖了大多数基础,并将帮助您找到最适合您的解决方案.
Sha*_*got 33
我只是简化了如何使用它来控制平滑滚动的答案.
创建类
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
public class CustomRecyclerView extends RecyclerView {
Context context;
public CustomRecyclerView(Context context) {
super(context);
this.context = context;
}
public CustomRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean fling(int velocityX, int velocityY) {
velocityY *= 0.7;
// velocityX *= 0.7; for Horizontal recycler view. comment velocityY line not require for Horizontal Mode.
return super.fling(velocityX, velocityY);
}
}
Run Code Online (Sandbox Code Playgroud)
通过将0.7替换为您的值来调整速度.
现在在这样的XML中使用这个类.
<<yourpackage>.CustomRecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
Run Code Online (Sandbox Code Playgroud)
阅读Java中的RecyclerView.
CustomRecyclerView mRecyclerView;
mRecyclerView = (CustomRecyclerView) findViewById(R.id.my_recycler_view);
Run Code Online (Sandbox Code Playgroud)
Blu*_*rer 28
只需实现LinearLayoutManager的smoothScrollToPosition():
LinearLayoutManager layoutManager = new LinearLayoutManager(this) {
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
LinearSmoothScroller smoothScroller = new LinearSmoothScroller(this) {
private static final float SPEED = 300f;// Change this value (default=25f)
@Override
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
return SPEED / displayMetrics.densityDpi;
}
};
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
62163 次 |
| 最近记录: |