如何在recyclerview过度滚动显示动画项目然后反弹?

时间:2018-02-01 21:29:21

标签: android android-recyclerview

我希望显示一个在过度滚动而不是默认色调叠加层上动画的项目。

会有一个小动画然后反弹回来。

这是完整的序列:

  1. 用户水平滑动以结束
  2. 用户过度滚动
  3. 显示的动画是标准项目的大小(我们使用lottie来显示动画)
  4. 在动画结束时,reyclerview反弹回未被过度滚动
  5. 谢谢!

1 个答案:

答案 0 :(得分:0)

我有一个reyclerview适配器,其中包含带动画的footerview。当用户在末尾附近滚动时,我会在页脚视图中启动animatinon,当它完成时我向后滚动reyclerview,使得页脚视图不在屏幕上。

private void configureFooterItemAnimation(RecyclerView itemRecyclerView, LinearLayoutManager linearLayoutManager,
        ProductRecyclerAdapter productRecyclerAdapter) {

    int productWidth = activity.getResources().getDimensionPixelSize(R.dimen.product_footer_width_plus_15);

    itemRecyclerView.addOnScrollListener(new OnScrollListener() {
        @Override
        public void onScrolled(final RecyclerView recyclerView, final int dx, final int dy) {
            super.onScrolled(recyclerView, dx, dy);

            if (recyclerView.getScrollState() == SCROLL_STATE_IDLE)  {
                return;
            }

            if (linearLayoutManager.findLastVisibleItemPosition() == productRecyclerAdapter.getItemCount() -1) {

                final LottieAnimationView footerView = productRecyclerAdapter.getFooterView();

                if (!footerView.isAnimating()) {
                    footerView.addAnimatorListener(new AnimatorListener() {
                        @Override
                        public void onAnimationStart(final Animator animation) {
                            itemRecyclerView.setOnTouchListener(getConsumeAllEventsOnTouchListener());
                            itemRecyclerView.scrollBy(dpToPx(activity, 230), 0);
                        }
                        @Override
                        public void onAnimationEnd(final Animator animation) {
                            itemRecyclerView.scrollBy(-productWidth, 0);
                            footerView.removeAllAnimatorListeners();
                            itemRecyclerView.setOnTouchListener(null);
                        }
                        @Override
                        public void onAnimationCancel(final Animator animation) {

                        }
                        @Override
                        public void onAnimationRepeat(final Animator animation) {

                        }
                    });
                    footerView.playAnimation();
                }
            }
        }
    });
}

@NonNull
private OnTouchListener getConsumeAllEventsOnTouchListener() {
    return new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return true;
        }
    };
}