动画被调用两次

时间:2017-01-07 16:35:24

标签: android listview

我有一个listView,我想从中删除一个项目。

用户从右向左滑动项目后开始动画。然后,应删除此项目。

我的代码:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
        if(convertView==null){
            convertView = layoutInflater.inflate(R.layout.note,null);
        }
        final RelativeLayout relativeLayout = (RelativeLayout) convertView.findViewById(R.id.relativeLayout);
        TextView textView = (TextView) convertView.findViewById(R.id.textView9);
        textView.setText(arrayList.get(position));
        Button button = (Button) convertView.findViewById(R.id.button10);
        button.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    x = event.getX();
                }
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (Math.abs(event.getX() - x) > (float)150) {
                        Log.e("Point_1", "Begin animate");
                        arrayList.remove(position);
                        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(relativeLayout, "x", 0 - relativeLayout.getWidth());
                        objectAnimator.setDuration(600);
                        objectAnimator.start();
                        Log.e("Point_1", "Start removing");

                        ListViewAdapter.this.notifyDataSetChanged();
                    }
                }
                return false;
            }
        });
}

当我运行我的应用程序几乎一切都很顺利。滑动后移除物品。删除视图的动画也很好。但!以某种方式在我的删除视图下面的项目也开始动画,在它之后(动画)它不会从listView中删除。怎么了?

删除前:

enter image description here

之后(3项 - 2篇文章):

enter image description here

1 个答案:

答案 0 :(得分:0)

如果有人会遇到这样的问题。

在项目的res文件夹中创建一个新目录“anim”。然后创建你想要的动画。在你的班级(-es)写下这些

final Animation animation1 = AnimationUtils.loadAnimation(context,R.anim."name of animation that you've already created");
animation1.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
            //Do whatever you've wanted to do. In my case I remove item from listView.

                arrayList.remove(position);
                ListViewAdapter.this.notifyDataSetChanged();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });