Currently I have a ListView with a TextView within each child. That TextView is taking in a Spanned string to populate it. I am using the ViewHolder pattern and I am not parsing the Spanned string in getView(). When I switch from a Spanned string back to just a plain string the performance of the ListView's scrolling increases dramatically. I would like to keep using Spanned because it will format my text correctly on the fly.
Is their a way to increase the performance of my ListView while keeping the string input for my TextViews Spanned?
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.comment, null);
holder = new ViewHolder();
holder.userName = (TextView) convertView.findViewById(R.id.comment_user_name);
holder.date = (TextView) convertView.findViewById(R.id.comment_created_date);
holder.body = (TextView) convertView.findViewById(R.id.comment_body);
convertView.setTag(holder);
} else {
开发者_运维百科 holder = (ViewHolder) convertView.getTag();
}
User user = array.get(position).getCreatedBy();
holder.userName.setText(user.getName());
holder.date.setText(array.get(position).getDateCreated());
holder.body.setText(array.get(position).getBody());
return convertView;
}
Although I did not find out the cause of the problem I did find a workaround to fix the problem noted above.
The call getBody() was returning a Spanned object which was causing an unusual amount of garbage collection with each iteration of getView(). When I converted the Spanned to a String the problem goes away. This is the code I modified to fix the problem.
holder.body.setText(array.get(position).getBody()**.toString()**);
Although I would still like to find a solid reason as to why this does occur.
精彩评论