在适配器中检测滚动方向(上/下)

20

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

当滚动时,列表视图的效果非常好,我想做类似的东西。

我已经开始使用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);

这似乎不太好,因为并非所有的元素都被动画化了:

因此,最终我使用了适配器的getView方法,并使用了以下代码:

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

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

不幸的是,只有当我从列表顶部向底部滚动时才有效!

我想让它在从另一侧滚动时也能正常工作,我需要稍微改变TranslateAnimation。

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


你的例子中,“row”变量是什么? - Bostone
您可以在此帖子中查看完整解决方案:https://dev59.com/L2Qn5IYBdhLWcg3we3PD#20091066 - thanhlcm
另一个可行的解决方案 https://dev59.com/5Gkw5IYBdhLWcg3wzdzZ#34788591 - Muhammad Babar
11个回答

0
view.setOnTouchListener(new View.OnTouchListener() {

        private long startClickTime;
        float y0 = 0;
        float y1 = 0;
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {

            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                y0 = motionEvent.getY();
                startClickTime = System.currentTimeMillis();

            } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                if (System.currentTimeMillis() - startClickTime < ViewConfiguration.getTapTimeout()) {

                    // Touch was a simple tap. Do whatever.

                } else {
                    y1 = motionEvent.getY();
                    // Touch was a not a simple tap.
                    if (y1 - y0 > 50) {
                        // this is down
                    } else if (y1 - y0 < 50) {
                        Log.d("daniY", "-");
                        // this is up
                    }
                }

            }

            return true;
        }

    });

这对我有用,我认为它可以在所有视图上检测滚动方向。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接