Android 为什么GridView看起来混淆了不同的单元格?
我的GridView由具有下载按钮的视图组成。点击按钮时,该按钮将隐藏,并显示进度条。按下下载按钮时启动的服务将更新进度条 我遇到的问题是,如果我开始下载某个特定元素,然后向下滚动,我会注意到进度条在不应该下载的单元格中可见。我认为这可能是由GridView回收电池造成的,但我不能完全确定。我在下面发布了一些代码。如果需要,我可以发布更多 getView-in适配器用于GridViewAndroid 为什么GridView看起来混淆了不同的单元格?,android,android-layout,listview,android-adapter,android-recyclerview,Android,Android Layout,Listview,Android Adapter,Android Recyclerview,我的GridView由具有下载按钮的视图组成。点击按钮时,该按钮将隐藏,并显示进度条。按下下载按钮时启动的服务将更新进度条 我遇到的问题是,如果我开始下载某个特定元素,然后向下滚动,我会注意到进度条在不应该下载的单元格中可见。我认为这可能是由GridView回收电池造成的,但我不能完全确定。我在下面发布了一些代码。如果需要,我可以发布更多 getView-in适配器用于GridView public View getView(final int position, View conver
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;
}
下载文件方法
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);
}
您需要在每个
getView()
调用上更新小部件可见性,而不仅仅是其中的一些调用。这与在每次调用getView()
时需要在小部件上设置文本或图像没有什么不同。AdapterView
和RecyclerView
类中的小部件,如GridView
,会被回收。如果在一个getView()
调用中显示ProgressBar
,并且该小部件被回收,并且您没有专门为回收的getView()
调用隐藏它,那么ProgressBar
仍将可见