My GridViewは、ダウンロードボタンを持つビューで構成されています。タップするとボタンが非表示になり、進行状況バーが表示されます。進行状況バーは、[ダウンロード]ボタンが押されたときに開始されるサービスによって更新されます。GridViewが異なるセルを混在させているように見えるのはなぜですか?
特定の要素のダウンロードを開始してからスクロールダウンすると、ダウンロードしてはならないセルにプログレスバーが表示されていることがわかります。私はこれがおそらくGridViewリサイクルセルに起因すると思うが、私は完全にはわからない。私は以下のコードをいくつか掲載しました。私は必要に応じてさらに投稿することができます。
getViewメソッド - アダプタ内のGridViewため
public View getView(final int position, View convertView, ViewGroup parent) {
final RelativeLayout card;
if (convertView == null) {
card = (RelativeLayout) getLayoutInflater().inflate(R.layout.book_cover, null);
} else {
card = (RelativeLayout) convertView;
}
TextView bookName = (TextView) card.findViewById(R.id.textView_bookName);
TextView authorName = (TextView) card.findViewById(R.id.textView_bookAuthor);
final MNBook currentBook = getItem(position);
bookName.setText((String) currentBook.getTitle());
authorName.setText((String) currentBook.getAuthor());
final Button downloadBookButton = (Button) card.findViewById(R.id.button_download_book);
final Button readBookButton = (Button) card.findViewById(R.id.button_read_book);
final ProgressBar progressBar = (ProgressBar) card.findViewById(R.id.progressbar_book_download);
readBookButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
intent.setDataAndType(Uri.fromFile(new File(getExternalFilesDir(null),
currentBook.getFileName())),
"application/pdf");
}
startActivity(intent);
}
});
downloadBookButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
downloadFile(currentBook, progressBar,
downloadBookButton, readBookButton);
}
});
if (currentBook.isBooleanDownloaded()) {
downloadBookButton.setVisibility(View.GONE);
readBookButton.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
}
// ImageView bgImage = (ImageView) card.findViewById(R.id.myImageView);
Resources resources = getApplicationContext().getResources();
// final int resourceId = resources.getIdentifier(mCategoryImages[position], "drawable",
// getApplicationContext().getPackageName());
// bgImage.setImageResource(resourceId);
return card;
}
あなたはすべてのgetView()
呼び出し、それらだけではなく、いくつかのウィジェットの視認性を更新する必要がありdownloadFile方法
private void downloadFile(MNBook book, final ProgressBar progressBar,
final Button downloadButton, final Button readButton) {
final MNBook localBook = book;
String fileName = book.getFileName();
String url = book.getURL();
Intent intent = new Intent(this, DownloadService.class);
intent.putExtra("url", url);
intent.putExtra("file_name", fileName);
intent.putExtra("receiver", new ResultReceiver(new Handler()) {
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
super.onReceiveResult(resultCode, resultData);
if (resultCode == DownloadService.UPDATE_PROGRESS) {
int progress = resultData.getInt("progress");
// mDetailFragment.setProgress(progress);
// mProgressDialog.setProgress(progress);
progressBar.setProgress(progress);
if (progress == 100) {
progressBar.setVisibility(View.GONE);
readButton.setVisibility(View.VISIBLE);
setDownloadedBookStatus(localBook);
}
}
}
});
downloadButton.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE);
startService(intent);
}