简体   繁体   English

滚动时,“回收者”视图项会设置动画

[英]Recycler view item animate when scrolling

I found and research many way and to custom recycler view item animate when scrolling (space between every item will stretch when scrolling) like this gif , even I referred to many related library such as recyclerview-animators or RecyclerViewItemAnimators or creating Animation for each holder onBindViewHolder(). 我发现并研究了多种方法,并且在滚动时自定义了回收站视图项目的动画(滚动时每个项目之间的空间都会拉伸),就像这样的gif ,甚至我也提到了许多相关的库,例如recyclerview-animatorsRecyclerViewItemAnimators或为onBindViewHolder中的每个持有者创建了Animation ()。 However, all ways above just create animation in the first time load view, can not implement like what animation I want to create. 但是,以上所有方法只能在首次加载视图中创建动画,而无法实现我想要创建的动画。 So anyone has done something like that, please help me to resolve that problem. 所以任何人都做了类似的事情,请帮助我解决该问题。 Thank you so much. 非常感谢。

EDIT: 编辑:

I tried to implement OnScrollListener method as @Android recommended. 我尝试按照@Android的建议实现OnScrollListener方法。 But it do not work properly. 但是它不能正常工作。 This code below show how I implemented them: 下面的代码显示了我如何实现它们:

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                int lastPosition = layoutManager.findLastCompletelyVisibleItemPosition();
                int firstPosition = layoutManager.findFirstCompletelyVisibleItemPosition();
                for (int i = firstPosition; i <= lastPosition; i++) {
                    if (dy < 0) {
                        AnimationUtils.animate(recyclerView.findViewHolderForAdapterPosition(i), false);
                    } else {
                        AnimationUtils.animate(recyclerView.findViewHolderForAdapterPosition(i), true);
                    }
                }
            }
        }); 

public class AnimationUtils { public static void animate(RecyclerView.ViewHolder viewHolder, boolean isGoDown){ ObjectAnimator translateY = ObjectAnimator.ofFloat(viewHolder.itemView, "translationY", isGoDown == true ? 300 : -300, 0); translateY.setDuration(300); translateY.setInterpolator(new DecelerateInterpolator(2f)); translateY.start(); } }

You can animate RecyclerView item like this. 您可以像这样为RecyclerView项目设置动画。
All you need is, to write an animate method and call that method in onBindViewHolder() method. 您所需onBindViewHolder()就是编写一个动画方法,然后在onBindViewHolder()方法中调用该方法。

Here is a example for Fading the RecyclerView item 这是使RecyclerView项褪色的示例

Method 方法

public void setFadeAnimation(View view) {
    AlphaAnimation anim = new AlphaAnimation(0.0f, 1.0f);
    anim.setDuration(500);
    view.startAnimation(anim);
}

Call this method 调用此方法

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {

    setFadeAnimation(holder.itemView);
    ........

}

This is the animation example. 这是动画示例。

You define the lastPosition 您定义lastPosition

private int lastPosition = -1;

private void setAnimation(View viewToAnimate, int position) {

    if (position > lastPosition) {
        Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left);
        animation.setDuration(1000);
        viewToAnimate.startAnimation(animation);
        lastPosition = position;
    } else if ( position < lastPosition) {
        Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left);
        viewToAnimate.startAnimation(animation);
        lastPosition = position;
    }
}

And then only you call the method onBindViewHolder 然后只有您调用onBindViewHolder方法

setAnimation(viewHolder.itemView, position);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM