Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restore the x offset of a listview item

Tags:

android

I am implementing a BaseAdapter and I have TranslateAnimation for a single list item.

The problem is that when scrolling down other views have the same offset.

Here is my implementation of the getView() method:

        @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;
  }

My question is::

How can I keep the View offset state for every ListView item?

like image 437
user1940676 Avatar asked May 12 '14 12:05

user1940676


1 Answers

The Android framework provides hooks to preserve and restore view properties, so all you need to do is to ensure that your view code implements onSaveInstanceState() and onRestoreInstanceState(Parcelable state) and when invoked, invokes the same for its child views and invokes the parent class' methods e.g. super.onSaveInstanceState().

I posted an answer here which in response to a question about retaining the data used within a ListView but also discusses how to preserve other view properties such as scroll position in the comments.

like image 104
Phil Haigh Avatar answered Oct 13 '22 09:10

Phil Haigh