Android在listview项目上下载progressbar
我正在编写一个简单的listview作为下载列表,用户可以点击它来下载它。我并没有任何问题,以下载文件与服务从互联网上,但在可见的进度栏上的列表视图项目。这就是显示整个listview,我无法处理这一点,即不显示整个listview,而只在单击的listview要下载的项目上显示progressbar 我的适配器:Android在listview项目上下载progressbar,android,listview,android-listview,Android,Listview,Android Listview,我正在编写一个简单的listview作为下载列表,用户可以点击它来下载它。我并没有任何问题,以下载文件与服务从互联网上,但在可见的进度栏上的列表视图项目。这就是显示整个listview,我无法处理这一点,即不显示整个listview,而只在单击的listview要下载的项目上显示progressbar 我的适配器: public void fillItems(final AdapterMessageContent adapter, final MessagesList item, final i
public void fillItems(final AdapterMessageContent adapter, final MessagesList item, final int position) {
stream_url = item.getStream_link().split("/");
stream_filename = stream_url[stream_url.length - 1];
File file = new File(G.dir_image + "/" + stream_filename);
if (item.getMsg_type() != 0) {
if (!file.exist()) {
Log.e("", "visible downloader icon");
UI.imgv_image_item.setImageBitmap(BitmapFactory.decodeFile(G.dir_app + "/" + "thumbs/" + item.getPreview_image_url()));
/* SET tag to handle this listview item */
UI.npb_progressbar.setTag(item.getId());
UI.iv_download_icon.setVisibility(View.VISIBLE);
UI.iv_download_icon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UI.ll_handle_progress_bar.setVisibility(View.VISIBLE);
item.setDownload_status(1);
G.DOWNLOADLIST.create(new DownloadTaskList(item.getMsg_id(), 0, item.getStream_link(), 0, 0, 0, position));
G.context.startService(new Intent(G.currentActivity, ServiceMultiTaskDownloader.class).putExtra("download_item_id", item.getMsg_id()));
UI.npb_progressbar.setVisibility(View.VISIBLE);
UI.tv_downloding_file_info.setVisibility(View.VISIBLE);
}
});
} if (UI.npb_progressbar.getTag().equals(item.getId())) {
UI.imgv_image_item.setImageBitmap(BitmapFactory.decodeFile(G.dir_app + "/" + item.getPreview_image_url()));
/* HIDE icon to disable click by user to download file */
UI.iv_download_icon.setVisibility(View.INVISIBLE);
}
}
}
现在,通过这个适配器,我可以通过简单的BroadCastReceiver更新行:
private void updateView(int position, int file_size, int downloaded_size, int percent, String stream_filename) {
View v = UI.lv_message_content.getChildAt(position - UI.lv_message_content.getFirstVisiblePosition());
UI.lv_message_content.getAdapter().getView(position, v, UI.lv_message_content);
UI.lv_message_content.invalidateViews();
if (v == null) return;
ImageView imgv_image_item = (ImageView) v.findViewById(R.id.imgv_image_item);
NumberProgressBar numberProgressBar = (NumberProgressBar) v.findViewById(R.id.npb_progressbar);
TextView tv_downloding_file_info = (TextView) v.findViewById(R.id.tv_downloding_file_info);
if (numberProgressBar.getTag() != null && numberProgressBar.getTag().equals(messagesLists.get(position).getId())) {
numberProgressBar.setProgress(percent);
tv_downloding_file_info.setVisibility(View.VISIBLE);
tv_downloding_file_info.setText(UC.convertByteToMB(downloaded_size) + "/" + UC.convertByteToMB(file_size));
if (percent == 100) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 30;
Bitmap bmp = BitmapFactory.decodeFile(G.dir_image + "/" + stream_filename);
imgv_image_item.setImageBitmap(bmp);
tv_downloding_file_info.setVisibility(View.GONE);
numberProgressBar.setVisibility(View.GONE);
}
}
}
不幸的是,当点击
UI.iv\u download\u图标下载文件时,我的listview项目在位置0和位置6重复出现。如果我正确理解了这一点,并且仔细阅读了代码,我认为这是一种误解适配器和listview
(或回收视图
)工作原理的情况
ListView
和新的RecyclerView
回收它们显示的视图,以最小化内存占用和视图创建开销。为了让列表中看起来有数百个视图,他们使用适配器
创建一些视图(足以填充列表中的区域),然后在向用户显示项目时更新视图的内容
如果您绕过适配器
的逻辑并修改属于适配器
的视图的内容,您将对列表上下看似随机的项进行相同的更改,因为发生了这种循环
解决此问题的方法是在适配器的getView
方法中进行视图更新(或RecyclerView.Adapter的onBindViewHolder
)。适配器
应该有一个内部数据列表,可以用来决定视图的外观。然后,您可以修改数据并调用notifyDataSetChanged
(使用RecyclerView.Adapter
时还有其他选项),这会导致为每个需要更新的项目调用getView
(在屏幕上可见)
故事的寓意:永远不要直接对列表中的视图进行视图更新,改为修改适配器中的数据
,并通知它数据已更改,以便它知道应该进行视图更新以显示最新数据。你在问什么?@Zharf不幸的是,当通过单击UI.iv_下载_图标下载文件时,listview项目上的my progressBar会在位置0和位置6重复。那么当你开始下载并向下滚动列表你在一个项目上找到了另一个不应该有进度条吗?@Zharf是的,先生,我无法处理这个问题,无法正确显示和找到必须显示进度条的项目