I have implemented GridView
in my app, using the BaseAdapter
. After following the recommended practices for any ListView
(use of ViewHolder
, recycling the View
and Universal Image Loader to load images asynchronously), I'm still not satisfied with the performance; the scrolling of the Grid is still not very smooth and the activity crashes if I scroll continuously up and down for about 5-6 seconds (without throwing any exception).
During the scrolling action I frequently get the following in my logcat -
I/Choreographer﹕ Skipped 32 frames! The application may be doing too much work on its main thread.
where 32 could also shoot up-to 54 frames.
Here is my code for the Cursor Adapter -
public class ContestantListCustomGridAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mLayoutInflater;
private Cursor allContestantShortInfo;
GlobalMethods gm = new GlobalMethods();
public ContestantListCustomGridAdapter(Context c) {
mContext = c;
String[] columns = new String[] {Database.ColumnOne,
Database.ColumnTwo,
Database.ColumnThree,
Database.ColumnFour,
Database.ColumnFive,
Database.ColumnSix,
};
//Get data from the DB
Database db = new Database(mContext);
db.open();
this.allContestantShortInfo = db.getValuesFromTable(Database.CONTESTANT_SHORT_INFO_TABLE,
columns, Database.CONTESTANT_SHORT_INFO_EVENT_ID + "=" + GlobalConstants.eventId,
null, null, null, null);
db.close();
allContestantShortInfo.moveToFirst();
mLayoutInflater = LayoutInflater.from(c);
}
@Override
public int getCount() {
return allContestantShortInfo.getCount();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
static class ViewHolder {
TextView participantName;
TextView participantCountry;
TextView participantLikes;
RelativeLayout participantEliminated;
ImageView participantImage;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.grid_view_card_layout, parent, false);
holder = new ViewHolder();
holder.participantEliminated = (RelativeLayout) convertView.findViewById(R.id.Card_isEliminated);
holder.participantName = (TextView) convertView.findViewById(R.id.grid_model_name);
holder.participantCountry = (TextView) convertView.findViewById(R.id.grid_country_name);
holder.participantLikes = (TextView) convertView.findViewById(R.id.participant_total_likes_number);
holder.participantImage = (ImageView) convertView.findViewById(R.id.grid_model_image);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
applyTypeFace(convertView);
//Set the values into placeholders
allContestantShortInfo.moveToPosition(position);
holder.participantName.setText(allContestantShortInfo
.getString(GlobalConstants.CONTESTANT_SHORT_INFO_NAME_INDEX));
holder.participantCountry.setText(allContestantShortInfo
.getString(GlobalConstants.CONTESTANT_SHORT_INFO_COUNTRY_NAME_INDEX));
holder.participantLikes.setText(allContestantShortInfo
.getString(GlobalConstants.CONTESTANT_SHORT_INFO_LIKES_INDEX));
if(allContestantShortInfo.getString(GlobalConstants.CONTESTANT_SHORT_INFO_ELIMINATION_STATUS_INDEX)
.equalsIgnoreCase("true")) {
holder.participantEliminated.setVisibility(View.VISIBLE);
holder.participantImage.setColorFilter(Color.parseColor("#90000000"));
} else {
holder.participantEliminated.setVisibility(View.GONE);
holder.participantImage.setColorFilter(0);
}
ImageLoader.getInstance()
.displayImage(allContestantShortInfo
.getString(GlobalConstants.CONTESTANT_SHORT_INFO_WEB_APP_PATH_INDEX) +
allContestantShortInfo
.getString(GlobalConstants.CONTESTANT_SHORT_INFO_THUMBNAIL_IMAGE_URL_INDEX),
holder.participantImage);
return convertView;
}
}
What could possibly be improved so that I can get my GridView
to be as smooth as any other ListView
and it does not crash too.