恢复listview项的x偏移量

use*_*676 5 android

我正在实现一个BaseAdapter,我有一个列表项的TranslateAnimation.

问题是当向下滚动时,其他视图具有相同的偏移量.

这是我的getView()方法实现:

        @Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;
    if (convertView == null) {
            //initialization code
            convertView.setTag(viewHolder);
    } else {
         viewHolder =(ViewHolder) convertView.getTag();
    }

        Animation animation;
        switch (item.getAnimationDirection()) {
        case AnimationDirection.HIDE:
            animation = new TranslateAnimation(itemOffset, 0, 0, 0);
            animation.setDuration(200);
            animation.setFillAfter(true);
            viewHolder.layoutToAnimate.setAnimation(animation);
            item.setAnimationDirection(AnimationDirection.HIDDEN);
            break;
        case AnimationDirection.REVEAL:
            itemOffset = viewHolder.remove.getWidth() + 20;
            animation = new TranslateAnimation(0, itemOffset, 0, 0);
            animation.setDuration(200);
            animation.setFillAfter(true);
            viewHolder.layoutToAnimate.setAnimation(animation);
            item.setAnimationDirection(AnimationDirection.SHOWING);
            break;
        }

        viewHolder.remove.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (item.getAnimationDirection() == AnimationDirection.HIDDEN) {
                    item.setAnimationDirection(AnimationDirection.REVEAL);
                    notifyDataSetChanged();
                }
            }
        });
        convertView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (item.getAnimationDirection() == AnimationDirection.SHOWING) {
                    item.setAnimationDirection(AnimationDirection.HIDE);
                    notifyDataSetChanged();
                }
            }
        });
        return convertView;
  }
Run Code Online (Sandbox Code Playgroud)

我的问题是:

如何保持View每个ListView项目的偏移状态?

nha*_*man 1

Map<Integer, Integer>为此使用 a 。就像是

map.put(position, offset);
Run Code Online (Sandbox Code Playgroud)

int offset = map.get(position);
Run Code Online (Sandbox Code Playgroud)

不要忘记每次调用时都getView进行设置。