下面是一段我写过的sample code,是一个小项目中的adapter的一部分,你可以参考下这个getView的思路来实现那个loading view。
public View getView(int pos, View convertView, ViewGroup parent)
{
Log.e(TAG, "[getView] Begin + This is position:" + pos);
final SongListActivity tempActivity = mParentActivity;
if (tempActivity == null)
{
return convertView;
}
if (mTrackListData == null)
{
return convertView;
}
WrapperSong curSong = null;
if (mTrackListData.size() > pos)
{
curSong = mTrackListData.get(pos);
}
else
{
if (mLoadingView == null)
{
mLoadingView = mInflater.inflate(R.layout.loading_item, null);
mLoadingView.setTag(FLAG_LOADING_ITEM);
}
mPrepareGettingMore = false;
mLoadingViewIsShown = true;
return mLoadingView;
}
return getViewWithPhoto(convertView, curSong, parent, pos);
}
// get list view with four text lines
View getViewWithPhoto(View convertView, WrapperSong curSong, ViewGroup parent, int pos)
{
if (curSong == null)
{
Log.e(TAG, "[getViewWithDescription] curSong cannot be null!");
return convertView;
}
ViewHolder holder;
if (convertView == null || !convertView.getTag().getClass().equals(ViewHolder.class))
{
Log.i(TAG, "convertView == null,Then inflate and new holder");
convertView = mInflater.inflate(R.layout.list_item, parent, false);
holder = new ViewHolder();
holder.listImageView = (ImageView) convertView.findViewById(R.id.ImageView);
holder.listTextView1 = (TextView) convertView.findViewById(R.id.TextView01);
holder.listTextView2 = (TextView) convertView.findViewById(R.id.TextView02);
holder.listTextView3 = (TextView) convertView.findViewById(R.id.TextView03);
convertView.setTag(holder);
}
else
{
Log.d(TAG, "convertView != null,Then get Holder");
holder = (ViewHolder) convertView.getTag();
}
Log.i(TAG, "convertView != null,Then SetValue");
if (holder != null && holder.listImageView != null)
{
holder.listImageView.setTag(new Integer(pos));
String name = curSong.getSongName();
holder.listTextView1.setText("[name]:" + name);
String artist = curSong.getSingerName();
holder.listTextView2.setText("[artist]:" + artist);
String desc = curSong.getSongDescription();
holder.listTextView3.setText("[description]:" + desc);
}
/*
* TODO:If need add cache , you can do something before add to
* decode The bitmap and decodePath will get from cache
*/
String songImageURL = curSong.getSongImage();
MBitmap item = mMemoryCacheBmp.pull(pos);
Bitmap bitmap = (item == null) ? null : item.getBitmap();
String decodePath = (item == null) ? null : item.getPath();
if (bitmap == null || bitmap.isRecycled()
|| (bitmap != null && !TextUtils.equals(decodePath, songImageURL)))
{
Log.d(TAG, "bitmap is null.then add to decode queue");
if (mDecoder != null)
{
Log.d(TAG, "pos:" + pos + ",songImageURL:" + songImageURL);
mDecoder.add(true, pos, songImageURL, pos, songImageURL);
}
else
{
Log.e(TAG, "[getViewWithDescription] mDecoder cannot be null!");
}
if (holder != null && holder.listImageView != null)
{
Log.e(TAG, "[getViewWithPhoto] Set Default Album Icon");
holder.listImageView.setImageBitmap(mDefaultBmp);
}
}
else
{
if (holder != null && holder.listImageView != null)
{
Log.e(TAG, "[getViewWithPhoto] Set Image From Cache");
holder.listImageView.setImageBitmap(bitmap);
}
}
Log.e(TAG, "[getView] end + This is position:" + pos);
return convertView;
}