有这样的问题,我有水平RecyclerView
,其中每个单元格都小于屏幕宽度。
所以我在这里找到了解决方法
RecyclerVIew auto scroll to display all the elements as in News Feed etc.,
如果一个单元格占据屏幕的整个宽度,否则所有工作都非常好(否则,如果每个单元格占据屏幕宽度的95%),则每次自动滑动都会将该单元格放置在屏幕的开头(右侧),这是合乎逻辑的。因此,在一个可见单元格的末尾是另一单元格的开始
看起来不太好。我需要这样的单元格位于屏幕的中间。
我需要查看上一个单元格-当前-下一个单元格
现在,我想向大家解释一些魔术,如何使当前的平滑滚动(如我在上面的链接中所述)
我的CustomLinnearLayoutManager
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position)
{
LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext())
{
@Override
public PointF computeScrollVectorForPosition(int targetPosition)
{
return SmoothLayoutManager.this.computeScrollVectorForPosition(targetPosition);
}
@Override
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics)
{
return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
}
};
linearSmoothScroller.setTargetPosition(position);
startSmoothScroll(linearSmoothScroller);
}
但是这种方法可以在没有偏移的情况下工作
我发现了另一种可以提供所需偏移量的方法
scrollToPositionWithOffset(final int position, final int offset)
这看起来确实正是我所需要的,但是这种方法无需平滑的动画即可工作。
所以,最终我的问题是:如何将动画逻辑从第一种方法应用到第二种方法(具有偏移)
随便问
答案 0 :(得分:2)
要自动捕捉并在find winner collection of your and store it to variable `winners`
winners.winner = winners.winner.reduce((acc, value) => {
acc.push({"data": value})
return acc;
}, []);
winners.save();
的中心显示一个项目,只需像下面这样使用RecyclerView
:
LinearSnapHelper
如果您要以编程方式滚动到特定项目,则LinearSnapHelper snapHelper = new LinearSnapHelper();
snapHelper.attachToRecyclerView(recyclerView);
也可以处理捕捉功能。
LinearSnapHelper
以下是视觉结果:
.................. 手动滚动 .................... ....... 以编程方式滚动 ..........
答案 1 :(得分:0)
最终,我发现了非常感谢@aminography的回答,并且还有一个答案对我有很大帮助
https://stackoverflow.com/a/39654328
实际上我现在有这样的实现方式
我的自定义-s
实现
LinnearLayoutManager
这就是我的设定方式
public class SmoothLayoutManager extends LinearLayoutManager
{
public static final int X_25 = 25;
public static final int X_200 = 200;
public static final float DEFAULT = X_25;
/**
* !! IMPORTANT !!
* If you need to add new value, don't forget add it here also
*/
@Retention(RetentionPolicy.SOURCE)
@IntDef({X_25, X_200})
private @interface Speed
{
}
private static float MILLISECONDS_PER_INCH = DEFAULT;
public SmoothLayoutManager(Context context)
{
super(context);
}
public SmoothLayoutManager(Context context, int orientation, boolean reverseLayout)
{
super(context, orientation, reverseLayout);
}
public SmoothLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
{
super(context, attrs, defStyleAttr, defStyleRes);
}
public SmoothLayoutManager setSpeedOfSmooth(@Speed int iSpeed)
{
MILLISECONDS_PER_INCH = iSpeed;
return this;
}
@Override
public void scrollToPositionWithOffset(final int position, final int offset)
{
super.scrollToPositionWithOffset(position, offset);
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position)
{
RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(recyclerView.getContext())
{
@Override
public PointF computeScrollVectorForPosition(int targetPosition)
{
return SmoothLayoutManager.this.computeScrollVectorForPosition(targetPosition);
}
@Override
protected int getVerticalSnapPreference()
{
return LinearSmoothScroller.SNAP_TO_ANY;
}
@Override
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics)
{
return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
}
@Override
public int calculateDtToFit(final int viewStart, final int viewEnd, final int boxStart, final int boxEnd, final int snapPreference)
{
return (boxStart + (boxEnd - boxStart) / 2) - (viewStart + (viewEnd - viewStart) / 2);
}
};
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
}
注意:
我注意到有时您快速滑动一下,所以
private void setRv(Context iC) { RecyclerView.Adapter adapter = new UpSaleInnerAdapter(mPicasso, mInflater, iLink -> mListener.onButtonClick(iLink)); mRv.setLayoutManager(new SmoothLayoutManager(iC, LinearLayoutManager.HORIZONTAL, false).setSpeedOfSmooth(SmoothLayoutManager.X_200)); mRv.setAdapter(adapter); SnapHelper snapHelper = new LinearSnapHelper(); snapHelper.attachToRecyclerView(mRv); }
有点困惑,并传递了更多需要...像超级模式的单元格:)如果有人会找到解决方法,请告诉我。
谢谢!