滚动时,Android Listview图像会随机播放

时间:2016-05-30 04:43:28

标签: android listview

我刚刚意识到当我滚动浏览列表视图时,我的图像会随意移动。我知道我应该检查onpostexecute区域中的位置,但是asynctask的输入参数与目前为止看到的示例不同。请帮忙。

private class AsyncDownloadTask extends AsyncTask<Object, String, Bitmap>{

    private String url;
    private View view;
    private Bitmap bitmap = null;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if(bitmap!=null&&view!=null){
            ImageView newsIcon = (ImageView)view.getTag(R.id.newsIcon);
            newsIcon.setImageBitmap(bitmap);
        }
    }

    @Override
    protected Bitmap doInBackground(Object... params) {
        view = (View)params[0];
        String uri = (String)params[1];
        try{
            InputStream inputStream = new URL(uri).openStream();
            bitmap = BitmapFactory.decodeStream(inputStream);
        }catch (Exception e){
            e.printStackTrace();
        }
        return bitmap;
    }
}

我的适配器

 @Override
public View getView(int position, View convertView, ViewGroup parent) {

    View row = convertView;
    if(position==0){

        NewsObj currNews = news.get(position);
        DataHandler dh;
        if(convertView==null){
            LayoutInflater inflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.firstnews,parent,false);
            dh = new DataHandler();
            dh.newsTitle = (TextView)row.findViewById(R.id.newsTitle);
            dh.newsDate = (TextView)row.findViewById(R.id.newsDate);
            dh.newsIcon = (ImageView)row.findViewById(R.id.newsIcon);
            row.setTag(dh);
        }else{
            dh = (DataHandler)row.getTag();
        }
        NewsObj no = (NewsObj)this.getItem(position);
        row.setTag(R.id.newsIcon,row.findViewById(R.id.newsIcon));
        new AsyncDownloadTask().execute(row,no.getImgurl());

        dh.newsTitle.setText(no.getTitle());
        dh.newsDate.setText(no.getDate());

    }else{

        NewsObj currNews = news.get(position);
        DataHandler dh;
        if(convertView==null){
            LayoutInflater inflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.newslist,parent,false);
            dh = new DataHandler();
            dh.newsTitle = (TextView)row.findViewById(R.id.newsTitle);
            dh.newsDate = (TextView)row.findViewById(R.id.newsDate);
            dh.newsIcon = (ImageView)row.findViewById(R.id.newsIcon);
            row.setTag(dh);
        }else{
            dh = (DataHandler)row.getTag();
        }
        NewsObj no = (NewsObj)this.getItem(position);
        row.setTag(R.id.newsIcon, row.findViewById(R.id.newsIcon));
        new AsyncDownloadTask().execute(row,no.getImgurl());

        dh.newsTitle.setText(no.getTitle());
        dh.newsDate.setText(no.getDate());

    }
    return row;
}

0 个答案:

没有答案