2

맞춤 RecyclerViewOnScrollListenerRecyclerView을 사용하고 있습니다. 아래로 스크롤 할 때 RecyclerView 항목 높이를 변경하고 위로 스크롤 할 때 원래 높이로 돌아가고 싶습니다.스크롤 할 때 어떻게 recyclerview 항목을 애니메이트 할 수 있습니까?

이미 동일한 수신기에서 bottomBar에 애니메이션을 적용하고 있지만 RecyclerView의 모든 항목에 애니메이션을 적용하는 방법을 이해할 수 없습니다. for 루프로 시도했지만 올바른 생각이라고 생각하지 않습니다.

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); 
} 

애니메이션입니다 :

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; 

} 

답변

0

당신의 ConvertView 아래와 같이하는 애니메이션을하는 방법을 정의합니다 (anime_content.xml를)

<?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" /> 

이 예에서는 마지막으로 부풀린 리사이클 뷰 행을 아래에서 위로 움직이게합니다. 어떻게하면 언제 까지나 애니메이션을 변경할 수 있습니다.

행운을 빌어 요!

+0

안녕하세요, 귀하의 답변에 감사드립니다. 이 구현은 처음으로 recyclerView 항목이 비정상적으로 작동하는 경우에만 작동합니다. onScroll에 응답하는 애니메이션이 필요했습니다. 위로 스크롤 할 때 높이를 높이거나 위로 스크롤 할 때 높이를 높이는 것처럼 – DoM4

+1

예. 이것은 recyclerview에 대한 애니메이션 구현을 부풀게합니다. – savepopulation

+0

reclyclerview를 스크롤 할 때 부풀려진 모습을 애니메이션화 할 무언가를 찾고있었습니다. – DoM4

관련 문제