ListView控件与进度对每一个项目? [英] listView with progressbar on every item?
问题描述
我有一个ListView和每一个项目我添加了一个进度条,需要消失的图像已被下载之后。但我无法找到一个方法来做到这一点。我试图使它在getView阶级消失,但它会立即消失,没有下载图片后。
例如,在添加上的AsyncTask类的一些看法,以滚动视图DoInBackground我可以下载的图像,然后onPostExecute我可以设置的图像,然后删除进度。这工作得很好。我想要做这样的事情的ListView控件。谁能帮我?
我不知道我是清楚的,或没有,但我可以总结一下,我有list_item.xml包含的ImageView和进度就可以了。我想使下载后,这些progressbars消失,设置图像。
感谢您的帮助。
这是我的适配器类:
类myListAdapter扩展ArrayAdapter<项目> {
私人的ArrayList<项目>项目;
私人上下文CTX;
公共myListAdapter(上下文的背景下,INT textViewResourceId,
ArrayList的<项目>项目){
超(背景下,textViewResourceId,项目);
this.ctx =背景;
this.items =项目;
}
@覆盖
公众诠释getCount将(){
返回items.size();
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
视图V = convertView;
如果(V == NULL){
LayoutInflater INF =(LayoutInflater)CTX
.getSystemService(LAYOUT_INFLATER_SERVICE);
V = inf.inflate(R.layout.main_list_item,NULL);
}
项指数= listContents.get(位置);
如果(指数!= NULL){
ImageView的IMG =(ImageView的)v.findViewById(R.id.listImage);
TextView的标题=(TextView中)v.findViewById(R.id.listTopText);
TextView的内容=(TextView中)v.findViewById(R.id.listDownText);
如果(标题!= NULL)
title.setText(index.getTitle());
如果(内容!= NULL)
content.setText(index.getContent());
如果(IMG!= NULL)
img.setImageBitmap(index.getImageBitmap());
((进度)v.findViewById(R.id.listItemProgressBar))setVisibility(View.GONE)。
}
返回伏;
}
}
那么,下面这条线,我做的进度条走了,但它应该下载的图像后消失,设置...但在列表项增加没有progressbars,这是我的问题?
((进度)v.findViewById(R.id.listItemProgressBar))setVisibility(View.GONE)。
您可以这样做,但它不是最好的做法。您应检查线程同步自己。和高速缓存的位图使用LruCache或WeakRefrences。该演示只显示了逻辑。
最后一类MyAdapter扩展了BaseAdapter {
私人最终的HashMap<字符串,位图> mImageMap;
私人最终的HashSet<字符串> mDownloadingSet;
公共MyAdapter(){
mImageMap =新的HashMap<字符串,位图>();
mDownloadingSet =新的HashSet<字符串>();
}
@覆盖
公众诠释getCount将(){
// TODO自动生成方法存根
返回NUMBER_YOU_WANT;
}
@覆盖
公共对象的getItem(INT位置){
// TODO自动生成方法存根
返回null;
}
@覆盖
众长getItemId(INT位置){
// TODO自动生成方法存根
返回0;
}
公共无效setImage(字符串URL,位图位图){
mDownloadingSet.remove(URL);
如果(位图!= NULL){
mImageMap.put(URL,位图);
notifyDataSetChanged();
}
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
ViewHolder持有人;
如果(convertView == NULL){
convertView = getLayoutInflater()。膨胀(R.layout.grid_view,
父母,假);
持有人=新ViewHolder();
/ ***
*找到了意见。
* /
convertView.setTag(保持器);
} 其他 {
支架=(ViewHolder)convertView.getTag();
}
最终的字符串URL =; //获取图像的URL在这里。
如果(mImageMap.containsKey(URL)){
holder.image.setImageBitmap(mImageMap.get(URL));
holder.progressBar.setVisibility(View.GONE);
} 其他 {
holder.image.setImageResource(R.drawable.img_downloading);
holder.progressBar.setVisibility(View.VISIBLE);
如果(!mDownloadingSet.contains(URL)){
ImageDownloader任务=新ImageDownloader();
mDownloadingSet.add(URL);
task.execute(URL);
}
}
返回convertView;
}
}
静态类ViewHolder {
ImageView的形象;
进度进度;
}
最后一类ImageDownloader扩展的AsyncTask<字符串,太虚,位图> {
字符串URL;
@覆盖
受保护的位图doInBackground(字符串... PARAMS){
网址= PARAMS [0];
最后的位图位= fetchUrlAndDe code(PARAMS [0]);
返回的位图;
}
@覆盖
保护无效onPostExecute(位图的结果){
mAdapter.setImage(URL,结果);
}
私人位图fetchUrlAndDe code(字符串URL){
点阵位图= NULL;
/ **
*获取你的位图和日code吧。
* /
返回的位图;
}
}
I've got a listView and on every item i added a progressBar which needs to disappear after image has been downloaded. But i couldn't find a way to do this. i tried to make it disappear on getView class, but it disappears immediately not after downloading image.
For example, while adding some views to scrollView on an AsyncTask class' DoInBackground i could download image and then onPostExecute i could set image, and then remove progressBar. This works just fine. I want to do something like this for listView. Could anyone help me?
I don't know if i was clear or not, but i can sum up that i have list_item.xml contains imageview and progressbar on it. And i want to make these progressbars disappear after downloaded and set images.
Thanks for helps.
Here's my adapter class:
class myListAdapter extends ArrayAdapter<Items> {
private ArrayList<Items> items;
private Context ctx;
public myListAdapter(Context context, int textViewResourceId,
ArrayList<Items> items) {
super(context, textViewResourceId, items);
this.ctx = context;
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inf = (LayoutInflater) ctx
.getSystemService(LAYOUT_INFLATER_SERVICE);
v = inf.inflate(R.layout.main_list_item, null);
}
Items index = listContents.get(position);
if (index != null) {
ImageView img = (ImageView) v.findViewById(R.id.listImage);
TextView title = (TextView) v.findViewById(R.id.listTopText);
TextView content = (TextView) v.findViewById(R.id.listDownText);
if (title != null)
title.setText(index.getTitle());
if (content != null)
content.setText(index.getContent());
if (img != null)
img.setImageBitmap(index.getImageBitmap());
((ProgressBar) v.findViewById(R.id.listItemProgressBar)).setVisibility(View.GONE);
}
return v;
}
}
So, on this line below, i make the progress bar gone but it should be gone after image downloaded and set... But on list items added there are no progressbars, that's my problem??
((ProgressBar) v.findViewById(R.id.listItemProgressBar)).setVisibility(View.GONE);
You can do like this, but it's not the best practise. you should check the Thread-Sync yourself. and Cache the bitmap use LruCache or WeakRefrences. the demo only show the logic.
final class MyAdapter extends BaseAdapter {
private final HashMap<String, Bitmap> mImageMap;
private final HashSet<String> mDownloadingSet;
public MyAdapter() {
mImageMap = new HashMap<String, Bitmap>();
mDownloadingSet = new HashSet<String>();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return NUMBER_YOU_WANT;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public void setImage(String url, Bitmap bitmap) {
mDownloadingSet.remove(url);
if (bitmap != null) {
mImageMap.put(url, bitmap);
notifyDataSetChanged();
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.grid_view,
parent, false);
holder = new ViewHolder();
/***
* find the views.
*/
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final String url = "";// Get the image url here.
if (mImageMap.containsKey(url)) {
holder.image.setImageBitmap(mImageMap.get(url));
holder.progressBar.setVisibility(View.GONE);
} else {
holder.image.setImageResource(R.drawable.img_downloading);
holder.progressBar.setVisibility(View.VISIBLE);
if (!mDownloadingSet.contains(url)) {
ImageDownloader task = new ImageDownloader();
mDownloadingSet.add(url);
task.execute(url);
}
}
return convertView;
}
}
static class ViewHolder {
ImageView image;
ProgressBar progressBar;
}
final class ImageDownloader extends AsyncTask<String, Void, Bitmap> {
String url;
@Override
protected Bitmap doInBackground(String... params) {
url = params[0];
final Bitmap bitmap = fetchUrlAndDecode(params[0]);
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
mAdapter.setImage(url, result);
}
private Bitmap fetchUrlAndDecode(String url) {
Bitmap bitmap = null;
/**
* Fetch your bitmap and decode it.
*/
return bitmap;
}
}
这篇关于ListView控件与进度对每一个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!