检测适配器中的滚动方向(上/下) [英] Detecting the scrolling direction in the adapter (up/down)

查看:26
本文介绍了检测适配器中的滚动方向(上/下)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的项目中模仿 Google Plus 应用程序,因为它现在似乎是参考.

I am trying to mimic the Google Plus application in my project, as it seems to be the reference now.

滚动时的列表视图效果非常好,我想做类似的事情.

The listview effect when scrolling is really nice and I would like to do something similar.

我已经开始使用 LayoutAnimationControllerhttp://android-er.blogspot.be/2009/10/listview-and-listactivity-layout.html

I have started with the LayoutAnimationController http://android-er.blogspot.be/2009/10/listview-and-listactivity-layout.html

LayoutAnimationController controller 
   = AnimationUtils.loadLayoutAnimation(
     this, R.anim.list_layout_controller);
  getListView().setLayoutAnimation(controller);

这看起来很糟糕,因为并非所有元素都具有动画效果:

and that seems bad, as not all the elements are animated:

所以我最终使用了适配器的 getView 并使用了这个:

So I ended up by using the getView of the adapter and using this:

        AnimationSet set = new AnimationSet(true);

        Animation animation = new AlphaAnimation(0.0f, 1.0f);
        animation.setDuration(800);
        set.addAnimation(animation);

        animation = new TranslateAnimation(
            Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f,
            Animation.RELATIVE_TO_SELF, 1.0f,Animation.RELATIVE_TO_SELF, 0.0f
        );
        animation.setDuration(600);
        set.addAnimation(animation);

        row.startAnimation(set);

结果很棒,我真的很满意!

The result is awesome and I am really happy with it!

不幸的是,它仅在我从列表顶部滚动到底部时才有效!

Unfortunately, it only works when I scroll from top to bottom of the list!

我想让它在另一边滚动时工作,我需要稍微改变一下 TranslateAnimation.

I want to make it work when scrolling on the other side, I need to change a little bit the TranslateAnimation.

所以我的问题是,有没有办法检测我是在适配器中向上还是向下滚动?

So my question, is there a way to detect if I scroll upwards or downwards in my adapter?

推荐答案

这是我遇到的最简单、最简单的方法.它就像一个魅力.

This is the easiest and simplest method I came across. And it works like a charm.

view.addOnScrollListener(new View.OnScrollListener() {
                @Override
                public void onScrolled(@NonNull View view, int dx, int dy) {
                    if (dy > 0) {
                        //Scrolling down
                    } else if (dy < 0) {
                        //Scrolling up
                    }
                }
            });

这篇关于检测适配器中的滚动方向(上/下)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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