[Android] Use the ViewHolder design pattern for the FolderBrowserAdapter. In directories with a lot of files, this should be noticeably smoother in terms of scrolling. Also fixed the case where the subtitle text might disappear.

This commit is contained in:
Lioncash 2013-11-16 03:56:58 -05:00
parent c90ce1aad1
commit 94f8c68a35
1 changed files with 32 additions and 12 deletions

View File

@ -27,9 +27,21 @@ import org.dolphinemu.dolphinemu.R;
*/
public final class FolderBrowserAdapter extends ArrayAdapter<FolderBrowserItem>
{
// ViewHolder which is used to hold onto
// items within a listview. This is done
// so that findViewById is not needed to
// be excessively called over and over.
private static final class ViewHolder
{
TextView title;
TextView subtitle;
ImageView icon;
}
private final Context context;
private final int id;
private final List<FolderBrowserItem> items;
private ViewHolder viewHolder;
/**
* Constructor
@ -60,42 +72,50 @@ public final class FolderBrowserAdapter extends ArrayAdapter<FolderBrowserItem>
{
LayoutInflater vi = LayoutInflater.from(context);
convertView = vi.inflate(id, parent, false);
// Initialize the ViewHolder and store it.
viewHolder = new ViewHolder();
viewHolder.title = (TextView) convertView.findViewById(R.id.ListItemTitle);
viewHolder.subtitle = (TextView) convertView.findViewById(R.id.ListItemSubTitle);
viewHolder.icon = (ImageView) convertView.findViewById(R.id.ListItemIcon);
convertView.setTag(viewHolder);
}
else // Can recover the holder.
{
viewHolder = (ViewHolder) convertView.getTag();
}
final FolderBrowserItem item = items.get(position);
if (item != null)
{
ImageView icon = (ImageView) convertView.findViewById(R.id.ListItemIcon);
TextView title = (TextView) convertView.findViewById(R.id.ListItemTitle);
TextView subtitle = (TextView) convertView.findViewById(R.id.ListItemSubTitle);
if(title != null)
if (viewHolder.title != null)
{
title.setText(item.getName());
viewHolder.title.setText(item.getName());
}
if(subtitle != null)
if (viewHolder.subtitle != null)
{
// Remove the subtitle for all folders, except for the parent directory folder.
if (item.isDirectory() && !item.getSubtitle().equals(context.getString(R.string.parent_directory)))
{
subtitle.setVisibility(View.GONE);
viewHolder.subtitle.setVisibility(View.GONE);
}
else
{
subtitle.setText(item.getSubtitle());
viewHolder.subtitle.setVisibility(View.VISIBLE);
viewHolder.subtitle.setText(item.getSubtitle());
}
}
if (icon != null)
if (viewHolder.icon != null)
{
if (item.isDirectory())
{
icon.setImageResource(R.drawable.ic_menu_folder);
viewHolder.icon.setImageResource(R.drawable.ic_menu_folder);
}
else
{
icon.setImageResource(R.drawable.ic_menu_file);
viewHolder.icon.setImageResource(R.drawable.ic_menu_file);
}
}
}