我有这样的观点:
ListView在整个屏幕上,按钮始终可见(在FrameLayout上)。当用户滚动列表并且他到达结束时(动画必须在他到达结束之前开始,因此它可以很好地移出屏幕),我想用动画隐藏屏幕外面的按钮(在它下面)。如何实现?
修改
感谢 PPartisan 我在onScroll上创建了一个非常好用的简单方法:
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
final int lastItem = firstVisibleItem + visibleItemCount;
if(lastItem == totalItemCount & button.getVisibility() == View.VISIBLE & hideAnimStarted == false) {
hideAnimStarted = true;
ObjectAnimator animator = ObjectAnimator.ofFloat(button, View.TRANSLATION_Y, 100);
animator.setDuration(300);
animator.start();
animator.addListener(new AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) { }
@Override
public void onAnimationRepeat(Animator animation) { }
@Override
public void onAnimationEnd(Animator animation) {
button.setVisibility(View.GONE);
}
@Override
public void onAnimationCancel(Animator animation) {}
});
}
else if(lastItem < totalItemCount & button.getVisibility() == View.GONE){
button.setVisibility(View.VISIBLE);
hideAnimStarted = false;
ObjectAnimator animator = ObjectAnimator.ofFloat(button, View.TRANSLATION_Y, 0);
animator.setDuration(300);
animator.start();
}
}
答案 0 :(得分:0)
我很少使用ListView
而非RecyclerView
,但RecyclerView
我通过实施OnInterceptTouchEvent
来实现这一目标。 ListView
也有the same method您可以覆盖。完成此操作后,您可以通过监听手指向下事件与更新后的位置之间的距离来检测滚动事件:
private static final int TOUCH_SLOP = 200; //Set this to your preference.
int originalFingerPosition;
//...
@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
switch (e.getAction()){
case MotionEvent.ACTION_DOWN:
originalFingerPosition = e.getY();
break;
case MotionEvent.ACTION_MOVE:
if (e.getY() - originalFingerPos > TOUCH_SLOP){
//This counts as a down swipe. Do something.
} else if (e.getY() + TOUCH_SLOP < originalFingerPosition ){
//This is an upswipe. Do something else.
}
}
return false;
}
一旦完成,请务必返回false,这样您就不会使用该事件并阻止其他视图按照滚动方式进行操作。
在此之后,只需启动合适的动画,例如TranslateAnimation
,即可在屏幕外滚动按钮。
修改强>
如果您只希望在到达列表的最后一项时发生动画,那么我会看看this答案,并在其中包含代码以设置Button
屏幕的动画:
改编自this帖子
中提供的示例listView.setOnScrollListener(OnScrollListener);
//...
@Override
public void onScroll(AbsListView lw, final int firstVisibleItem,
final int visibleItemCount, final int totalItemCount) {
switch(lw.getId()) {
case android.R.id.list:
final int lastItem = firstVisibleItem + visibleItemCount;
if((lastItem == totalItemCount) && (button.getVisibility() == View.VISIBLE)) {
Animation translate = new TranslateAnimation(
originalXPosition, originalXPosition, originalYPosition, listView.getHeight());
translate.setDuration(250);
translate.setInterpolator(new LinearInterpolator());
button.startAnimation(translate);
button.setEnabled(false);
button.setVisibility(View.GONE);
} else {
//The Opposite...
}
}
}