我正在尝试在列表视图中按长项目显示动态复选框。我能够成功获取复选框,而select all也可以。但我觉得代码非常复杂,导致多次调用getView。任何人都可以建议一个更好的实施方式吗? 另外,我在getView中的异步任务中获取albumArt,并且由于动态复选框多次调用getView,我在Logcat中多次获取GC释放msgs。这是我的代码:
的活动:
songView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
longClickpressed = true;
chkSelectAll.setVisibility(View.VISIBLE);
songAdt.notifyDataSetChanged();
return true;
}
});
chkSelectAll.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < songAdt.getCount(); i++) {
songList.get(i).setSelected(chkSelectAll.isChecked());
}
songAdt.selectAll(chkSelectAll.isChecked());
songAdt.unCheckAll(chkSelectAll.isChecked());
songAdt.notifyDataSetChanged();
}
});
适配器类:
public class SongAdapter extends BaseAdapter{
int countChecked = 0;
private boolean checkable = false;
boolean selectAll2 = false;
boolean checkable2 = true;
int countreload=-1;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
checkBoxCounter = 0;
checkBoxInitialized = 0;
View view = convertView;
final int pos = position;
if (view == null) {
final ViewHolder holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) parent.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.song, parent, false);
holder.tvTitle = (TextView) view.findViewById(R.id.tv_song_title);
holder.tvArtist = (TextView) view.findViewById(R.id.tv_song_artist);
holder.chkItem = (CheckBox) view.findViewById(R.id.chkLvItem);
holder.imgAlbumArt = (ImageView) view
.findViewById(R.id.img_lv_album_art);
holder.chkItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
countChecked++;
countreload++;
} else {
countChecked--;
countreload--;
}
Song element1 = (Song) holder.chkItem.getTag();
element1.setSelected(((CheckBox) v).isChecked());
setTitleActivity(countChecked);
if (checkBoxCounter <= checkBoxInitialized) {
// increment counter, when we scroll the List it
// execute onCheckedChanged everytime so by
// using this stuff we can maintain the state
checkBoxCounter++;
} else {
Song element2 = (Song) holder.chkItem.getTag();
element2.setSelected(((CheckBox) v).isChecked());
}
}
});
view.setTag(holder);
holder.chkItem.setTag(songs.get(position));
} else {
((ViewHolder) view.getTag()).chkItem.setTag(songs.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.tvArtist.setText(songs.get(position).getArtist());
holder.tvTitle.setText(songs.get(position).getTitle());
if (checkable) {
holder.chkItem.setVisibility(View.VISIBLE);
if (selectAll2) {
countChecked = songs.size();
songs.get(position).setSelected(selectAll2);
holder.chkItem.setChecked(selectAll2);
setTitleActivity(countChecked);
} else if (!selectAll2 && !checkable2 && countreload==-1) {
countChecked = 0;
songs.get(position).setSelected(selectAll2);
holder.chkItem.setChecked(selectAll2);
setTitleActivity(countChecked);
}
}
holder.chkItem.setChecked(songs.get(position).isSelected());
long albumId = songs.get(position).getAlbumId();
long songId = songs.get(position).getAudioId();
if (BitmapWorkerAsyncTask.cancelPotentialWork(albumId,
holder.imgAlbumArt)) {
final BitmapWorkerAsyncTask task = new BitmapWorkerAsyncTask(
holder.imgAlbumArt, mContext);
final BitmapWorkerAsyncTask.AsyncDrawable asyncDrawable = new BitmapWorkerAsyncTask.AsyncDrawable(
mContext.getResources(), mLoadingBitmap, task);
holder.imgAlbumArt.setImageDrawable(asyncDrawable);
task.execute(view, songId, albumId, mContext, 1);
}
return view;}
public void unCheckAll(boolean checkable1) {
checkable2 = checkable1;
}
public void selectAll(boolean selectAll) {
selectAll2 = selectAll;
}