Reputation: 446
My Problem: An item in my ListView animates after I begin scrolling. It is not the last item and animation should be finished. After I have scrolled once the problem is gone, It only happens once and on a single item in the list.
How can I stop that single item from animating after I begin to scroll the ListView?
I want the animation to run only once when an item is added to the adapter.
I have a list adapter in which I am animating items as they are added to a list. I am using a declared animator to slide in the items from the left in the getView() method of my adapter. Here is my getView() method where the animation is applied.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String listItemText = mList.get(position);
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater li = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.mListItemText = (TextView) convertView.findViewById(R.id.progress_title);
Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.slide_in_left);
convertView.startAnimation(animation);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mListItemText.setText(listItemText);
return convertView;
}
private static class ViewHolder {
public TextView mListItemText;
}
Upvotes: 1
Views: 3157
Reputation: 5950
convertView is null the first time that the list try to build a row. so your animation start only the first time and in the first item.
What you want is to animate an item if, for example, a flag is set to true.
An example:
To check if an item is new i'll do in this way:
public void initList() {
/* Change your mList to get Bundle instead of String */
mList = new ArrayList<Bundle>();
}
/* When you add an item you will put a bundle with your string and a flag that tell the item is new */
public void addItem(String item) {
Bundle bundle = new Bundle();
bundle.putString("string", item);
bundle.putBoolean("new", true);
mList.add(bundle);
}
Now check if the object is new:
public View getView(int position, View convertView, ViewGroup parent) {
Bundle item = mList.get(position);
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater li = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.mListItemText = (TextView) convertView.findViewById(R.id.progress_title);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mListItemText.setText(item.getString("string"));
if(item.getBoolean("new") == true) {
Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.slide_in_left);
convertView.startAnimation(animation);
item.setBoolean("new", false);
}
return convertView;
}
Upvotes: 2