android–Listview在投掷和滚动时显示错误视图几秒钟

android–Listview在投掷和滚动时显示错误视图几秒钟,第1张

概述系统似乎是回收视图,直到它在我的列表视图中加载正确位置的视图,导致重复的图像和文本几秒钟.有人可以帮忙吗?@OverridepublicViewgetView(finalintposition,ViewconvertView,ViewGroupparent){Viewv=convertView;Log.d("position",""+position);if(

系统似乎是回收视图,直到它在我的列表视图中加载正确位置的视图,导致重复的图像和文本几秒钟.有人可以帮忙吗?

@OverrIDepublic VIEw getVIEw(final int position, VIEw convertVIEw, VIEwGroup parent) {    VIEw v = convertVIEw;    Log.d("position",""+position);    if(v==null){        LayoutInflater inflater = LayoutInflater.from(mContext);        v = inflater.inflate(R.layout.layout_appinfo, null);        holder = new VIEwHolder();        holder.ivAppIcon = (ImageVIEw)v.findVIEwByID(R.ID.ivIconApp);        holder.tvAppname = (TextVIEw)v.findVIEwByID(R.ID.tvnameApp);        holder.progress = (Progressbar)v.findVIEwByID(R.ID.progress_spinner);        v.setTag(holder);    } else {        holder = (VIEwHolder) v.getTag();    }    holder.ivAppIcon.setimageDrawable(null);    holder.tvAppname.setText(null);    holder.progress.setVisibility(VIEw.VISIBLE);    holder.ivAppIcon.setVisibility(VIEw.GONE);    // Using an AsyncTask to load the slow images in a background thread    new AsyncTask<VIEwHolder, VoID, Drawable>() {        private VIEwHolder v;        private ResolveInfo entry = (ResolveInfo) mListAppInfo.get(position);        @OverrIDe        protected Drawable doInBackground(VIEwHolder... params) {            v = params[0];            return entry.loadIcon(mPackManager);        }        @OverrIDe        protected voID onPostExecute(Drawable result) {            super.onPostExecute(result);            // If this item hasn't been recycled already, hIDe the            // progress and set and show the image            v.progress.setVisibility(VIEw.GONE);            v.ivAppIcon.setVisibility(VIEw.VISIBLE);            v.ivAppIcon.setimageDrawable(result);            v.tvAppname.setText(entry.loadLabel(mPackManager));        }    }.execute(holder);    return v;}static class VIEwHolder {      TextVIEw tvAppname;      ImageVIEw ivAppIcon;      Progressbar progress;      //int position;}

这几乎就好像位置设置错了几秒钟.

解决方法:

您可以让VIEwHolder保存对AsyncTask的引用.当convertVIEw!= null时,您可以在VIEwHolder持有的AsyncTask上调用cancel(),因为您知道它正在加载的图像对于这个新行不正确.在doInBackgrond()和onPostExecute()中,首先检查是否(!isCancelled())然后再做任何事情.

static class VIEwHolder {    TextVIEw tvAppname;    ImageVIEw ivAppIcon;    Progressbar progress;    AsyncTask task;}public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) {    VIEwHolder holder;    if (convertVIEw == null) {        /* inflate new vIEw, make new VIEwHolder, etc. */        ...    } else {        holder = (VIEwHolder) convertVIEw.getTag();        holder.task.cancel();    }    // always make a new AsyncTask, they cannot be reused    holder.task = new AsyncTask ...    holder.execute(...);    ...}
总结

以上是内存溢出为你收集整理的android – Listview在投掷和滚动时显示错误视图几秒钟全部内容,希望文章能够帮你解决android – Listview在投掷和滚动时显示错误视图几秒钟所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1097084.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存