【问题标题】:Android horizontal RecycleView with never-ending scroll animation具有永无止境的滚动动画的 Android 水平 RecyclerView
【发布时间】:2020-07-21 10:24:30
【问题描述】:

我正在尝试在我的 Android 应用程序中实现一个水平 RecycleView,以显示具有永无止境的滚动动画的内容。 See example of required UI here.

我的问题是,如何实现这一目标的最佳做法是什么? (RecycleView 应该支持来自用户的滚动手势)。

【问题讨论】:

  • 如果动画到达内容结尾,内容是否应该从头开始重复?我认为这将是一个比 gif 中显示的更优雅的解决方案。让动画跳转到一个不流畅的UX。

标签: android android-recyclerview android-animation


【解决方案1】:

如果我是正确的,您正在寻找无限滚动或无限滚动(不是官方名称

如果它是正确的,那么当用户到达最终项目时,它会显示你的 recyclerview 现在有 5 张图片,然后 recycler 视图会从第一个项目重新开始,比如

item1, item2, item3, item4, item5 和 item5, item1, item2.....

实现这一目标

转到您的适配器类并执行以下代码。

@Override
    public int getItemCount() {
        return items == null ? 0 : items.size() * 2; //Here instead of items.size(); you have to change like this
    }

现在在您的 OnBindViewHolder 方法中

MODEL_CLASS item = items.get(position%items.size());

现在转到将适配器设置为 recyclerview 的活动并添加以下代码

YOUR_ADAPTER_NAME.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int firstItemVisible = linearLayoutManager.findFirstVisibleItemPosition();
                if (firstItemVisible != 0 && firstItemVisible % YOUR_LIST_ITEM.size() == 0) {
                    recyclerView.getLayoutManager().scrollToPosition(0);
                }
            }
        });

这样,您可以在向右滚动时看到项目,它会在您的列表中继续循环。

【讨论】:

  • 对我来说最重要的部分是动画,如何实现它..在你的回答中根本没有提到动画..
  • 但是我在该图像中没有找到任何动画效果。我可以看到你只是慢慢地滑动图像。如果有任何动画效果请说明我会尽力回答
  • @OdedRegev 使用“recyclerView.getLayoutManager().smoothScrollToPosition(recyclerView,RecyclerView.State(),0);”为了更流畅的滚动..
猜你喜欢
  • 1970-01-01
  • 2019-09-18
  • 2019-04-16
  • 1970-01-01
  • 2016-10-10
  • 2015-12-08
  • 1970-01-01
  • 1970-01-01
  • 2016-10-26
相关资源
最近更新 更多