Aks*_*AB' 25 android android-fragments android-viewpager android-recyclerview
我尝试在回收器视图上添加Scroll监听器并制作了一些逻辑,但我无法一次刷一个项目.我在互联网上做了一些搜索,但我得到了一些第三方库,它有定制的回收站视图.我们可以在回收站视图中一次实施一个项目滑动吗?如果是,请告诉我怎么做?一次扫描一次,就像这张图片一样.
Pal*_*alm 23
我知道,这已经很晚了.
有一种非常简单的方法可以使用自定义SnapHelper准确获取所请求的滚动行为.
通过覆盖标准的(android.support.v7.widget.LinearSnapHelper)创建自己的SnapHelper.
public class SnapHelperOneByOne extends LinearSnapHelper{
@Override
public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY){
if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)) {
return RecyclerView.NO_POSITION;
}
final View currentView = findSnapView(layoutManager);
if( currentView == null ){
return RecyclerView.NO_POSITION;
}
final int currentPosition = layoutManager.getPosition(currentView);
if (currentPosition == RecyclerView.NO_POSITION) {
return RecyclerView.NO_POSITION;
}
return currentPosition;
}
}
Run Code Online (Sandbox Code Playgroud)
这基本上是stadard方法,但是没有添加通过滚动速度计算的跳转计数器.
如果快速和长按,下一个(或上一个)视图将居中(显示).
如果您慢速和短暂滑动,当前居中的视图会在释放后保持居中.
我希望这个答案仍然可以帮助任何人.
小智 10
这可以软化项目之间的移动:
public class SnapHelperOneByOne extends LinearSnapHelper {
@Override
public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY) {
if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)) {
return RecyclerView.NO_POSITION;
}
final View currentView = findSnapView(layoutManager);
if (currentView == null) {
return RecyclerView.NO_POSITION;
}
LinearLayoutManager myLayoutManager = (LinearLayoutManager) layoutManager;
int position1 = myLayoutManager.findFirstVisibleItemPosition();
int position2 = myLayoutManager.findLastVisibleItemPosition();
int currentPosition = layoutManager.getPosition(currentView);
if (velocityX > 400) {
currentPosition = position2;
} else if (velocityX < 400) {
currentPosition = position1;
}
if (currentPosition == RecyclerView.NO_POSITION) {
return RecyclerView.NO_POSITION;
}
return currentPosition;
}
}
Run Code Online (Sandbox Code Playgroud)
例:
LinearSnapHelper linearSnapHelper = new SnapHelperOneByOne();
linearSnapHelper.attachToRecyclerView(recyclerView);
Run Code Online (Sandbox Code Playgroud)
https://github.com/googlesamples/android-HorizontalPaging/
这具有与您在图像中显示的内容类似的链接。如果您正在寻找其他东西,请告诉我,我将链接相关的库。
ViewPager 和 recyclerView 之间的基本上区别在于,在 recyclerView 中您在许多项目之间切换,而在 ViewPager 中您在许多片段或独立页面本身之间切换。
我看到您正在使用这个https://github.com/lsjwzh/RecyclerViewPager,您是否有任何特定的用例?
| 归档时间: |
|
| 查看次数: |
17833 次 |
| 最近记录: |