【发布时间】:2016-11-01 13:37:15
【问题描述】:
我正在使用 RecyclerView 和自定义 RecyclerViewOnScrollListener。
我想在向下滚动时更改 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;
}
【问题讨论】:
-
你找到解决办法了吗?
标签: android animation android-recyclerview onscrolllistener