靠近ListView的末端时设置动画/隐藏按钮 [英] Animate/hide button when close to end of ListView

查看:124
本文介绍了靠近ListView的末端时设置动画/隐藏按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的视图:


ListView在整个屏幕上,并且该按钮始终可见(在FrameLayout上)。当用户滚动列表并且到达结尾时,我想用动画将按钮隐藏在屏幕下方(下方)(动画必须在到达结尾之前开始动画,这样才能很好地移出屏幕)。如何实现?

ListView is on the whole screen and the button is always visible (on FrameLayout). I want to hide the button outside the screen (below it) with animation when user is scrolling list and he's reaching the end (animation must start before he reaches the end so it could nicely move out of the screen). How to achieve that?

编辑

感谢 PPartisan 我创建了一个运行良好的简单方法onScroll:

Thanks to PPartisan I created simple method onScroll which is working very well:

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


推荐答案

I很少在 RecyclerView 上使用 ListView ,但是通过 RecyclerView 我实现了这可以通过实现 OnInterceptTouchEvent 来实现。 ListView 还具有您可以覆盖的相同方法。完成此操作后,可以通过侦听手指按下事件与移动后的更新位置之间的距离来检测滚动事件:

I rarely use ListView over RecyclerView, but with RecyclerView I achieve this by implementing OnInterceptTouchEvent. ListView also has the same method that you can Override. Once you've done this, you can detect scroll events by listening for the distance between the finger down event, and the updated position once it has moved:

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,以免消耗

Be sure to return false once you're done so you don't consume the event and prevent other views from behaving as they should with a scroll.

之后,这只是启动合适的动画的一种情况,例如 TranslateAnimation ,以在屏幕外滚动按钮。

After that, it's just a case of launching a suitable animation, like a TranslateAnimation, to scroll the button off screen.

编辑:
如果只希望当您到达列表的最后一项时,动画就会出现,那么我将改为查看这个答案,并包括在屏幕外的 Button 动画的代码:

If you only want the animation to occur when you reach the last item of your list, then I would instead take a look at this answer and include the code to animate the Button off screen there:

改编自发布

Adapted from the example given in this post

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...
          }
     }
}

这篇关于靠近ListView的末端时设置动画/隐藏按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆