Question

I have a ListView with a Button in it that animates other Views within a list item. Although I am using the holder pattern it does take too much time to reset the positions of these animated list items whenever the view is re-used. Does anyone know how to improve the performance without letting go of the Animation? It's not like the list is unuseable, but there is always a little stutter while scrolling, whenever a new listitem comes into the screen. Here is the (abbreviated) getView() code:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    final SuggestionViewHolder holder;

    final Suggestion suggestion = getItem(position);


    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_suggestion, parent, false);

        holder = new SuggestionViewHolder();

        holder.content =  = (ViewGroup) convertView.findViewById(R.id.content);
        holder.openExternalLinksButton = (ImageButton) convertView.findViewById(R.id.btn_open_external_links);
        holder.externalLinks = (ViewGroup) convertView.findViewById(R.id.external_links);
        holder.closeExternalLinksButton = (ImageView) convertView.findViewById(R.id.btn_close_external_links);           

        convertView.setTag(holder);

    } else {
        holder = (SuggestionViewHolder) convertView.getTag();

        // reset values for re-use of holder
        holder.content.setScaleX(1);
        holder.content.setScaleY(1);
        holder.content.setAlpha(1);
        holder.content.setBlocked(false);
        holder.externalLinks.setTranslationY(0);        
    }        

    // animate external links to show them
    holder.openExternalLinksButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View anchor) {
            holder.content.animate().scaleX(0.9f).scaleY(0.9f).alpha(0.3f).start();
            holder.externalLinks.animate().setDuration(220).translationYBy(mExternalLinkWrapperTranslationY);
        }
    });

    // animate external links to hide them
    holder.closeExternalLinksButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            holder.content.animate().setDuration(220).scaleX(1f).scaleY(1f).alpha(1f).start();
            holder.externalLinks.animate().setDuration(220).translationY(0);
        }
    });       

    return convertView;
}

What takes much time is the "reset values for re-use of holder"-part.

Thanks!

Was it helpful?

Solution

I had the same problem...

I find this article that help me to improve the performance of the alpha functionality https://plus.google.com/+RomanNurik/posts/NSgQvbfXGQN

Just put

View.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

before the animation

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top