Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 滚动时如何设置recyclerview项目的动画?_Android_Animation_Android Recyclerview_Onscrolllistener - Fatal编程技术网

Android 滚动时如何设置recyclerview项目的动画?

Android 滚动时如何设置recyclerview项目的动画?,android,animation,android-recyclerview,onscrolllistener,Android,Animation,Android Recyclerview,Onscrolllistener,我正在将RecyclerView与自定义的RecyclerViewScrollListener一起使用。 我想在向下滚动时更改RecyclerView项目高度,在向上滚动时返回原始高度 我已经在同一个侦听器中设置底部栏的动画,但我不知道如何在RecyclerView中设置每个项的动画。我试过使用for循环,但我认为这不是正确的想法 public abstract class RecyclerViewOnScrollListener extends RecyclerView.OnScrollLis

我正在将
RecyclerView
与自定义的
RecyclerViewScrollListener
一起使用。 我想在向下滚动时更改
RecyclerView
项目高度,在向上滚动时返回原始高度

我已经在同一个侦听器中设置底部栏的动画,但我不知道如何在RecyclerView中设置每个项的动画。我试过使用for循环,但我认为这不是正确的想法

public abstract class RecyclerViewOnScrollListener extends RecyclerView.OnScrollListener {

    private LinearLayout bottomBarContainer;
    private boolean animateItems = false;
    CardStackLayoutManager cardStackLayoutManager;
    public RecyclerViewOnScrollListener(LinearLayout bottomBarContainer) {
        this.bottomBarContainer = bottomBarContainer;
    }

    public RecyclerViewOnScrollListener(LinearLayout bottomBarContainer, boolean animateItems, CardStackLayoutManager cardStackLayoutManager) {
        this.bottomBarContainer = bottomBarContainer;
        this.animateItems = animateItems;
        this.cardStackLayoutManager = cardStackLayoutManager;
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);

        if (bottomBarContainer==null || dy==0 ) {
            return;
        }
        long ANIMATION_DURATION = 200L;
        if (dy>0) { // Scrolling to bottom

            if (mIsScrollDirectionLocked && mScrollingDirection!=0) return;

            if (bottomBarContainer.getVisibility()== View.GONE || mIsAnimatingOff) {
                return;
            } else {
                for(int i = 0;i < cardStackLayoutManager.getChildCount();i++)
                {
                    View view = cardStackLayoutManager.getChildAt(i);
                    ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view,"y",0f,200f);
                    objectAnimator.setDuration(150);
                    objectAnimator.start();
                }
                mScrollingDirection = SCROLLING_DOWN;
                mIsAnimatingOff = !mIsAnimatingOff;

                ViewCompat.setTranslationY(bottomBarContainer, 0F);

                ViewCompat.animate(bottomBarContainer)
                        .translationY(bottomBarContainer.getHeight())
                        .setDuration(ANIMATION_DURATION)
                        .setListener(new ViewPropertyAnimatorListenerAdapter() {
                            @Override
                            public void onAnimationEnd(View view) {
                                mIsAnimatingOff = !mIsAnimatingOff;
                                bottomBarContainer.setVisibility(View.GONE);
                            }
                        }).start();

            }
        } else { // Scrolling to top
            if (mIsScrollDirectionLocked && mScrollingDirection!=0) return;

            if (bottomBarContainer.getVisibility()!=View.VISIBLE && !mIsAnimatingOn) {

                mScrollingDirection = SCROLLING_UP;
                mIsAnimatingOn = !mIsAnimatingOn;
                bottomBarContainer.setVisibility(View.VISIBLE);
                for(int i = 0;i < cardStackLayoutManager.getChildCount();i++)
                {
                    View view = cardStackLayoutManager.getChildAt(i);
                    ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view,"y",200f,0f);
                    objectAnimator.setDuration(150);
                    objectAnimator.start();
                }
                ViewCompat.setTranslationY(bottomBarContainer, bottomBarContainer.getHeight());

                ViewCompat.animate(bottomBarContainer)
                        .translationY(0F)
                        .setDuration(ANIMATION_DURATION)
                        .setListener(new ViewPropertyAnimatorListenerAdapter() {
                            @Override
                            public void onAnimationEnd(View view) {
                                mIsAnimatingOn = !mIsAnimatingOn;
                            }
                        }).start();
            }
        }
    }

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        if (!mIsScrollDirectionLocked) return;

        switch (newState) {
            case RecyclerView.SCROLL_STATE_IDLE:
                mScrollingDirection = 0;
                break;
            default:
                break;
        }
    }

    private static final int SCROLLING_UP = 1;

    private static final int SCROLLING_DOWN = 2;

    private int mScrollingDirection = 0;

    private boolean mIsScrollDirectionLocked = false;

    private boolean mIsAnimatingOff = false;

    private boolean mIsAnimatingOn = false;

}
公共抽象类RecycleWebOnScrollListener扩展了RecycleView.OnScrollListener{
专用线路布置箱;
私有布尔animateItems=false;
CardStackLayoutManager CardStackLayoutManager;
公共RecycleServiceOnScrollListener(LinearLayout bottomBarContainer){
this.bottomBarContainer=bottomBarContainer;
}
public RecycleServiceOnScrollListener(LinearLayout bottomBarContainer、boolean animateItems、CardStackLayoutManager CardStackLayoutManager){
this.bottomBarContainer=bottomBarContainer;
this.animateItems=animateItems;
this.cardStackLayoutManager=cardStackLayoutManager;
}
@凌驾
已填空的公共空间(RecyclerView RecyclerView、int dx、int dy){
super.onScrolled(recyclerView、dx、dy);
if(bottomBarContainer==null | | dy==0){
返回;
}
长动画持续时间=200L;
如果(dy>0){//滚动到底部
如果(mIsScrollDirectionLocked&&mScrollingDirection!=0)返回;
if(bottomBarContainer.getVisibility()==View.GONE | | mIsAnimatingOff){
返回;
}否则{
对于(int i=0;i
定义一种方法来设置
转换视图的动画,如下所示:

private void setAnimation(View viewToAnimate,int position) {
      if (position > lastPosition) {
         lastPosition = position;
         Animation animation = AnimationUtils.loadAnimation(activity, R.anim.anim_content);
         viewToAnimate.startAnimation(animation);
      }
}
在适配器的
onBindViewHolder
方法中调用动画方法:

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
     // Do your initialization
     setAnimation(holder.convertView, position);
}
下面是动画:(anime_content.xml)


此示例将从下至上设置上次充气回收器视图行的动画。您可以随时更改动画


祝你好运

嗨,谢谢你的回答。此实现仅在recyclerView项第一次膨胀时才起作用。我想要一些动画响应onScroll..比如向下滚动时增加高度,向上滚动时减少是的,这是一个用于RecycleView的行膨胀动画实现。我正在寻找在滚动RecycleService软件时为膨胀视图设置动画的东西。你找到解决方案了吗?
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromYDelta="25%p"
    android:toYDelta="0%p"
    android:duration="300"
    android:interpolator="@android:anim/decelerate_interpolator" />