Animate RecyclerView when scrolling

前端 未结 3 1422
夕颜
夕颜 2020-12-24 08:02

Is there any way to animate the elements of a RecyclerView when I scroll it?

I took a look at DefaultItemAnimator and RecyclerView.ItemAnimator

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-24 08:33

    I ended in using an OnScrollListener and animating it in a custom animate() method. In my case that code takes just 2ms so that is no problem for the 60fps.

    recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(int newState) {
            if(newState == RecyclerView.SCROLL_STATE_IDLE) {
                // special handler to avoid displaying half elements
                scrollToNext();
            }
            animate();
        }
    
        @Override
        public void onScrolled(int dx, int dy) {
            animate();
        }
    });
    

提交回复
热议问题