【问题标题】:Android ListView Animation weird behaviorAndroid ListView 动画奇怪的行为
【发布时间】:2013-01-08 02:34:07
【问题描述】:

我想在我的列表视图中为 onFling 事件设置动画。

动画效果很好,但在错误的行。

例如:

  1. ListView 只有一项,动画按预期工作。
  2. ListView 有两个项目:手势是在第一个项目上进行,第二个项目是动画,反之亦然。
  3. 具有三个项目的 ListView:在第一个项目上做出手势,最后一个是动画的,在第二个项目上,第二个项目是动画的(如预期的那样),在最后一个项目上,第一个项目是动画的。

代码:

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    int position = listView.pointToPosition(Math.round(e1.getX()),
            Math.round(e1.getY()));
    View row = listView.getChildAt(position);
    TranslateAnimation anim = null;
    if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH || row == null)
        return true;
    if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
            && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
        anim = new TranslateAnimation(Animation.RELATIVE_TO_SELF,1,Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0);
        this.handleSwipe.onHandleSwipeLeft(position);
    } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
            && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
        anim = new TranslateAnimation(Animation.RELATIVE_TO_SELF,-1,Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0);
        this.handleSwipe.onHandleSwipeRight(position);
    }else{
        return true;
    }

    anim.setDuration(500);
    row.startAnimation(anim);
    return true;
}

OBS:

已经调试好了,位置和行总是对的。

我正在使用 7 级 API。

代码在 android 4.2 上运行良好

【问题讨论】:

    标签: android android-listview android-animation


    【解决方案1】:

    Guilherme,大概是代码:

        int position = listView.pointToPosition(Math.round(e1.getX()),
            Math.round(e1.getY()));    
    

    没有返回行 id 3。你能确认一下吗?

    【讨论】:

    • 正如我所说,不!有返回预期值。我几乎可以肯定这是一个android BUG。在 api >= 11 上,此代码工作正常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2019-10-09
    • 2016-08-29
    • 1970-01-01
    • 1970-01-01
    • 2010-10-08
    相关资源
    最近更新 更多