Android 如何改进ListView的外观
目前我面临以下问题-我创建了一个自定义的列表视图(1xImageView+2xTextView)。它显示来自I-net的内容,I-net是一个XML文件 我在做什么-我正在使用AsyncTask在后台运行下载XML内容和填充ListView的过程,方法是调用publishProgress(…)方法,从doInBackground(…)方法中为每个列表项调用publishProgress(…)方法。当doInBackground(…)完成时,我已经收集了我想要显示的每个图像的URL,然后从onPostExecute(…)方法开始图像下载过程,并更新每个下载图像的UI。我的问题是,在开始下载可绘制文件并更新列表之前,列表视图应该至少填充一些项目。当前,列表视图显示单个项目,当填充下一个项目时,所有图像都已下载。我调试了这个应用程序,发现有一个我停在onPostExecute(…)方法中,想要获取ListView项目的数量,它只返回一个,就像它显示的那样 在开始下载之前,是否有办法强制外观列表视图的项目,或者这是因为我拥有的I-net连接足够强大,并且下载图像不会造成延迟 我忘了只提一下,每一张图片都不比10KB大 这是我的自定义任务:Android 如何改进ListView的外观,android,listview,user-interface,Android,Listview,User Interface,目前我面临以下问题-我创建了一个自定义的列表视图(1xImageView+2xTextView)。它显示来自I-net的内容,I-net是一个XML文件 我在做什么-我正在使用AsyncTask在后台运行下载XML内容和填充ListView的过程,方法是调用publishProgress(…)方法,从doInBackground(…)方法中为每个列表项调用publishProgress(…)方法。当doInBackground(…)完成时,我已经收集了我想要显示的每个图像的URL,然后从onPo
private class DownloadFilesTask extends AsyncTask<String, Object, List<RSSItem>> {
protected void onPreExecute() {
super.onPreExecute();
la.clear();
}
protected List<RSSItem> doInBackground(String... urls) {
parse(urls[0]);
if (feed == null) { return null; }
rssList = feed.getAllItems();
Iterator<RSSItem> iterator = rssList.iterator();
while(iterator.hasNext()) {
if (isCancelled()) return null;
RSSItem rss = iterator.next();
publishProgress(rss);
}
return rssList;
}
protected void onProgressUpdate(Object... progress) {
super.onProgressUpdate(progress);
la.add((RSSItem)progress[0]);
la.notifyDataSetChanged();
}
protected void onPostExecute(List<RSSItem> result) {
super.onPostExecute(result);
Drawable downloadedImage;
for (int z = 0; z < rssList.size(); z++) {
downloadedImage = downloadImage(rssList.get(z).getImageURL());
((RSSItem)rssList.get(z)).setImage(downloadedImage);
la.notifyDataSetChanged();
}
}
protected void onCancelled() {
super.onCancelled();
}
}
private class ListAdapter extends ArrayAdapter<RSSItem> {
private List<RSSItem> items;
public ListAdapter(Context context, int textViewResourceId, List<RSSItem> items) {
super(context, textViewResourceId, items);
this.items = items;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_item, null);
}
RSSItem item = items.get(position);
if (item != null) {
ImageView itemImage = (ImageView) v.findViewById(R.id.ivImage);
TextView title = (TextView) v.findViewById(R.id.tvTitle);
TextView description = (TextView) v.findViewById(R.id.tvDescription);
if (item.getImage() != null) {
itemImage.setBackgroundDrawable(item.getImage());
} else {
itemImage.setBackgroundDrawable(getResources().getDrawable(R.drawable.icon));
}
if (title != null) {
title.setText(item.getTitle());
}
if (description != null) {
description.setText(item.getDescription());
}
}
return v;
}
}
@修改:
私有类下载文件任务扩展异步任务>{
受保护的void onPreExecute(){
super.onPreExecute()
la.clear();
}
受保护列表doInBackground(字符串…URL){
解析(URL[0]);
如果(feed==null){returnnull;}
rssList=feed.getAllItems();
出版进度(真实);
可绘图下载图像;
对于(intz=0;z
是否在doInBackground()
中填充列表适配器的基础数据集?然后,除了调用publishProgress()
,您还需要在AsyncTask的onProgressUpdate()
中调用adapter.notifyDataSetChanged()
是否在doInBackground()
中填充列表适配器的基础数据集?然后,除了调用publishProgress()
,您还需要在AsyncTask的onProgressUpdate()
中调用adapter.notifyDataSetChanged()
,我不确定我是否理解您的问题,但我要做的是以下几点(如果我正确的话):
我假设您在ListView中存储了一个自定义对象,比如CustomItem。
我会将URL图像的下载放在该对象的构造函数中,并将Drawable
存储为CustomItem的成员。在创建新的CustomItem
并获取其映像之后,publishProgress()
并在列表适配器上调用notifyDataSetChanged()
编辑:您应该从doInBackground()
方法末尾的onPostExecute()
方法中移动代码
试试这个:
private class DownloadFilesTask extends AsyncTask<String, RSSItem, Void> {
protected void onPreExecute() {
super.onPreExecute();
la.clear();
}
protected List<RSSItem> doInBackground(String... urls) {
parse(urls[0]);
if (feed == null) { return null; }
rssList = feed.getAllItems();
Iterator<RSSItem> iterator = rssList.iterator();
while(iterator.hasNext()) {
if (isCancelled()) return null;
RSSItem rss = iterator.next();
//this happens fast no need to do this
publishProgress(rss);
//la.add(rss);
}
Drawable downloadedImage;
for (int z = 0; z < rssList.size(); z++) {
downloadedImage = downloadImage(rssList.get(z).getImageURL());
((RSSItem)rssList.get(z)).setImage(downloadedImage);
publishProgress(null);
}
return rssList;
}
protected void onProgressUpdate(RSSItem... progress) {
super.onProgressUpdate(progress);
if progress!=null
la.add((RSSItem)progress[0]);
la.notifyDataSetChanged();
}
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
protected void onCancelled() {
super.onCancelled();
}
}
私有类下载文件任务扩展异步任务{
受保护的void onPreExecute(){
super.onPreExecute();
la.clear();
}
受保护列表doInBackground(字符串…URL){
解析(URL[0]);
如果(feed==null){returnnull;}
rssList=feed.getAllItems();
迭代器迭代器=rssList.Iterator();
while(iterator.hasNext()){
if(isCancelled())返回null;
RSSItem rss=iterator.next();
//这发生得很快,不需要这样做
出版进度(rss);
//添加(rss);
}
可绘图下载图像;
对于(intz=0;z
我不确定我是否理解您的问题,但我会做以下几点(如果我做对了):
我假设您在ListView中存储了一个自定义对象,比如CustomItem。
我会将URL图像的下载放在该对象的构造函数中,并将Drawable
存储为CustomItem的成员。在创建新的CustomItem
并获取其映像之后,publishProgress()
并在列表适配器上调用notifyDataSetChanged()
编辑:您应该从doInBackground()
方法末尾的onPostExecute()
方法中移动代码
试试这个:
private class DownloadFilesTask extends AsyncTask<String, RSSItem, Void> {
protected void onPreExecute() {
super.onPreExecute();
la.clear();
}
protected List<RSSItem> doInBackground(String... urls) {
parse(urls[0]);
if (feed == null) { return null; }
rssList = feed.getAllItems();
Iterator<RSSItem> iterator = rssList.iterator();
while(iterator.hasNext()) {
if (isCancelled()) return null;
RSSItem rss = iterator.next();
//this happens fast no need to do this
publishProgress(rss);
//la.add(rss);
}
Drawable downloadedImage;
for (int z = 0; z < rssList.size(); z++) {
downloadedImage = downloadImage(rssList.get(z).getImageURL());
((RSSItem)rssList.get(z)).setImage(downloadedImage);
publishProgress(null);
}
return rssList;
}
protected void onProgressUpdate(RSSItem... progress) {
super.onProgressUpdate(progress);
if progress!=null
la.add((RSSItem)progress[0]);
la.notifyDataSetChanged();
}
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
protected void onCancelled() {
super.onCancelled();
}
}
私有类下载文件任务扩展异步任务{
上的保护无效
private class DownloadFilesTask extends AsyncTask<String, RSSItem, Void> {
protected void onPreExecute() {
super.onPreExecute();
la.clear();
}
protected List<RSSItem> doInBackground(String... urls) {
parse(urls[0]);
if (feed == null) { return null; }
rssList = feed.getAllItems();
Iterator<RSSItem> iterator = rssList.iterator();
while(iterator.hasNext()) {
if (isCancelled()) return null;
RSSItem rss = iterator.next();
//this happens fast no need to do this
publishProgress(rss);
//la.add(rss);
}
Drawable downloadedImage;
for (int z = 0; z < rssList.size(); z++) {
downloadedImage = downloadImage(rssList.get(z).getImageURL());
((RSSItem)rssList.get(z)).setImage(downloadedImage);
publishProgress(null);
}
return rssList;
}
protected void onProgressUpdate(RSSItem... progress) {
super.onProgressUpdate(progress);
if progress!=null
la.add((RSSItem)progress[0]);
la.notifyDataSetChanged();
}
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
protected void onCancelled() {
super.onCancelled();
}
}