RecyclerView左右滑动切换速度处理
前段时间项目新增功能需要做个画廊效果、但做好后产品提了个小要求说:能在滑动的时候速度慢一些嚒。
你听到内心在滴血了嚒?
看来以后码代码的时候旁边需要放一把大刀才行
不过网上无意中看到一篇博客貌似叫什么画廊的,后来才无意中看到的自定义RecyclerView控制fling速度:
xml
<com.mesio.applibs.views.SpeedRecyclerView
android:id="@+id/recycler_loan"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.mesio.applibs.views.SpeedRecyclerView>
自定义RecyclerView
/**
* 控制fling速度的RecyclerView
*/
public class SpeedRecyclerView extends RecyclerView {
private static final float FLING_SCALE_DOWN_FACTOR = 0.5f; // 减速因子
private static final int FLING_MAX_VELOCITY = 3000; // 最大顺时滑动速度
public SpeedRecyclerView(Context context) {
super(context);
}
public SpeedRecyclerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public SpeedRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean fling(int velocityX, int velocityY) {
velocityX = solveVelocity(velocityX);
velocityY = solveVelocity(velocityY);
return super.fling(velocityX, velocityY);
}
private int solveVelocity(int velocity) {
if (velocity > 0) {
return Math.min(velocity, FLING_MAX_VELOCITY);
} else {
return Math.max(velocity, -FLING_MAX_VELOCITY);
}
}
}
根据自己需求调整FLING_MAX_VELOCITY的值,可控制滑动速度
群号:574730787
原文链接:https://blog.csdn.net/llixiangjian/article/details/83834936
本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。
还没有人抢沙发呢~