chenlittleping / recyclercoverflow Goto Github PK
View Code? Open in Web Editor NEW使用RecyclerView,自定义LayoutManager实现旋转木马相册效果
License: Apache License 2.0
使用RecyclerView,自定义LayoutManager实现旋转木马相册效果
License: Apache License 2.0
你好,我用的平板横屏调试demo,出现这个问题。
我debug看了下,
位置0->位置1,正常调用smoothScrollToPosition
位置1->位置2,调用smoothScrollToPosition同时会调用scrollHorizontallyBy,导致mOffsetAll重新计算,导致滚动失败。
我尝试着在scrollHorizontallyBy 和 fixOffsetWhenFinishScroll方法里加个判断,return出去不触发,恢复正常使用。具体原因分析不出来。
我的修改:
` @OverRide
public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler,
RecyclerView.State state) {
if (isSmoothScroll){
Log.e("isSmoothScroll" , "return");
return 0;
}
if (mAnimation != null && mAnimation.isRunning()) mAnimation.cancel();
int travel = dx;
if (!mIsLoop) { //非循环模式,限制滚动位置
if (dx + mOffsetAll < 0) {
travel = -mOffsetAll;
} else if (dx + mOffsetAll > getMaxOffset()){
travel = (int) (getMaxOffset() - mOffsetAll);
}
}
mOffsetAll += travel; //累计偏移量
layoutItems(recycler, state, dx > 0 ? SCROLL_TO_LEFT : SCROLL_TO_RIGHT);
return travel;
}
/**
* 修正停止滚动后,Item滚动到中间位置
/
private void fixOffsetWhenFinishScroll() {
if (isSmoothScroll){
Log.e("smoothScroll","return fixOffsetWhenFinishScroll");
return;
}
int scrollN = (int) (mOffsetAll * 1.0f / getIntervalDistance());
float moreDx = (mOffsetAll % getIntervalDistance());
double judgeDistance = getIntervalDistance() * 0.5;
if (Math.abs(moreDx) > judgeDistance) {
if (moreDx > 0) scrollN ++;
else scrollN --;
}
int finalOffset = (int) (scrollN * getIntervalDistance());
startScroll(mOffsetAll, finalOffset);
mSelectPosition = Math.abs(Math.round (finalOffset * 1.0f / getIntervalDistance())) % getItemCount();
}
/*
* 滚动到指定X轴位置
* @param from X轴方向起始点的偏移量
* @param to X轴方向终点的偏移量
*/
private isSmoothScroll = false;
private void startScroll(int from, int to) {
if (mAnimation != null && mAnimation.isRunning()) {
mAnimation.cancel();
}
final int direction = from < to ? SCROLL_TO_LEFT : SCROLL_TO_RIGHT;
mAnimation = ValueAnimator.ofFloat(from, to);
mAnimation.setDuration(500);
mAnimation.setInterpolator(new DecelerateInterpolator());
mAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@OverRide
public void onAnimationUpdate(ValueAnimator animation) {
mOffsetAll = Math.round((float) animation.getAnimatedValue());
layoutItems(mRecycle, mState, direction);
}
});
mAnimation.addListener(new Animator.AnimatorListener() {
@OverRide
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
onSelectedCallBack();
isSmoothScroll = false;
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
mAnimation.start();
}
@OverRide
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
// TODO 循环模式暂不支持平滑滚动
if (mIsLoop) return;
int finalOffset = calculateOffsetForPosition(position);
if (mRecycle == null || mState == null) {//如果RecyclerView还没初始化完,先记录下要滚动的位置
mSelectPosition = position;
} else {
startScroll(mOffsetAll, finalOffset);
isSmoothScroll = true;
}
}
`
当删除其中某一张图片时,如果是从后面往前删除后不会默认滑动到它的前张,而且删除最后一张的时候会出现下标越界异常。adapter.notifyDataSetChangerd()后就会出现上述情况
故障复现:
1)将item间隔因数设置为0.62f, 同时将RecyclerCoverFlow高度设置为140dp,宽度充满. adapteer的item宽高设置为120dp. 这样刚好让屏幕上只显示5个item.
2)故障规律:在滑动RecyclerCoverFlow停止后,如果停止item position正好是5的倍数余1.(是以Demo中position从1开始计算) bug就会复现.
例如每屏item数量为5个, Demo滑动停止后的item position为6, 11 ,16, 21,31,36等等, 最中间的item都会被右边的item (第7,12 , 17, 22, 32,37)所盖住一部分,也就是最中间的item层级不是在最上层.
补充: 将间隔因数设置为0.6f,就不会复现这种问题
怎么设置循环滚动;然后只显示3个;其余的隐藏
需求: 在屏幕上只想显示五个item.
解决方案: 根据自身item的宽和设置item间隔因数,来调整显示在屏幕上面的个数. 设定好只显示五个item.
但是由于屏幕的像素密度不一致的问题. 会导致部分手机上显示不同, 可能会显示七个或是更多.
例如乐视 Le x260手机 1dp = 2.625px. 它比其它手机可能会多显示两个或是更多.
推荐使用张鸿洋的AndroidAutoLayout库.
https://github.com/hongyangAndroid/AndroidAutoLayout
对它的item进行尺寸适配, 所有屏幕基上就长的一样了, 这样下来就可以完美解决了
Fatal Exception: java.lang.NullPointerException
Attempt to invoke virtual method 'boolean androidx.recyclerview.widget.RecyclerView$State.isPreLayout()' on a null object reference
recycler.coverflow.CoverFlowLayoutManger.layoutItems (CoverFlowLayoutManger.java:184)
recycler.coverflow.CoverFlowLayoutManger.access$300 (CoverFlowLayoutManger.java:28)
recycler.coverflow.CoverFlowLayoutManger$1.onAnimationUpdate (CoverFlowLayoutManger.java:448)
android.animation.ValueAnimator.animateValue (ValueAnimator.java:1558)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:950)
how to make indicator with the recycle cover flow?
i'm tried to add to LinePagerIndicatorDecoration when using addItemDecoration, after casting the layout with CoverFlowLayoutManger, i have a problem when findFirstVisibleItemPosition !!!!
how to make a Dots indicator decoration in the bottom
如题
显示到固定的位置调用 scrollTopPosition 无效
这个效果做成竖直轮播的,能实现吗,该怎么做
是否可以考虑下使用google提供的SnapHelper
CoverFlowLayoutManger中mSlectPositon计算存在bug
private void onSelectedCallBack() {
mSelectPosition = Math.round (mOffsetAll / getIntervalDistance());
mSelectPosition = Math.abs(mSelectPosition % getItemCount());
if (mSelectedListener != null && mSelectPosition != mLastSelectPosition) {
mSelectedListener.onItemSelected(mSelectPosition);
}
mLastSelectPosition = mSelectPosition;
}
当mSelectPositon为负数时,会出现位置对不上,比如我有5个条目,当mSelectPosition为-6,Math.abs(mSelectPosition % getItemCount())计算结果为1,但事实对应的条目应该是4,当mSelectPosition为负数时正确的位置计算应该为应为
mSelectPosition = 条目个数和(list.size())+mSelectPosition % getItemCount()
RT,在嵌套 ViewPager 使用时,横向滚动冲突。
请添加item的点击事件 谢谢
开启平面滚动,view的回收复用不协调
最中间的最大那个item与左右两个的比例
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.