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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With