图像在Android中滚动时改变位置

时间:2013-08-08 18:02:54

标签: android

在我的Android应用程序中,我从网址中获取图像并在网格视图中显示它。但是当我向下滚动时,图像会随机改变位置。我正在使用异步线程来获取图像并更新图像视图对象。任何建议如何解决这个问题......

我已经提到问题Images in gridview change their position on scrolling in android,但它没有帮助我


My code for getView and loading the image
@Override
        public View getView(final int position, View convertView, ViewGroup parent) {
                final HashMap<String, String> song = songsList.get(position);
                imageURL = song.get(VariablesList.TAG_ALBUM_IMAGE);
                View v = null;
                if (convertView != null)
                        v = convertView;
                else
                        v = inflater.inflate(R.layout.gridlayout_item, parent, false);
                final ImageView imageView = (ImageView) v.findViewById(R.id.icon_image);


                       new LoadImage().execute(imageURL,imageView);

                imageView.setOnClickListener(new View.OnClickListener() {
                        // @Override
                        public void onClick(View v) {
                                albumUrl = new StringBuffer(resources.getString(R.string.songsListURL));
                                String albumIndex = song.get("id");
                                albumName = (song.get("name"));
                                imageURL = song.get(VariablesList.TAG_ALBUM_IMAGE);
                                SongsList albumList = new SongsList(imageURL, albumUrl,
                                                albumName,albumIndex,resources);
                                Thread threadAlbumList = new Thread(albumList);
                                threadAlbumList.start();

                                synchronized (albumList) {
                                        try {
                                                albumList.wait();
                                        } catch (InterruptedException e) {
                                                e.printStackTrace();
                                        }
                                }
                                if (!NewMediaPlayer.mediaPlayer.isPlaying()) {
                                        HashMap<String, String> playingSong = NewMediaPlayer.selectedSongs
                                                        .get(0);
                                        if (playingSong != null) {
                                                String url = playingSong.get("songUrl");
                                                String songName = playingSong.get("songName");
                                                if (songName != null)
                                                        {
                                                         NewMediaPlayer.songTitleLabel.setText(albumName
                                                                        + " - " + songName);
                                                         NewMediaPlayer.songTitle.setText(albumName+"-"+songName);
                                                        }
                                                NewMediaPlayer.playSong(url);
                                        }
                                }
                        }
                });

                TextView itemAlbumName = (TextView) v.findViewById(R.id.icon_text);
                itemAlbumName.setText(song.get("name"));
                itemAlbumName.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                                HashMap<String, String> song = songsList.get(position);
                                String songIndex = song.get("id");
                                String albumName = (song.get("name"));
                                Intent in = new Intent(context, SongListActivity.class);
                                in.putExtra("albumIndex", songIndex);
                                in.putExtra("albumName", albumName);
                                in.putExtra("AlbumImage", song.get(VariablesList.TAG_ALBUM_IMAGE));
                                context.startActivity(in);
                        }
                });

                return v;
        }


public void LoadImage extends AsyncTask<Object, Void, Bitmap>{

ImageView imageView;
   @Override
    protected BitMap doInBackground(Object... urls) {
    String imageURL = (String)urls[0];
    imageView = (ImageView)urls[0];
    BitMap image = AlbumList.LoadImagetoGridView(imageURL)
    return image
}

@Override
    protected void onPostExecute(BitMap result) {
    imageView.setImageBitmap(result);
}
}

2 个答案:

答案 0 :(得分:0)

滚动时,请不要取消之前的图像加载 首先,阅读android tutorial以了解这种情况 其次,请使用图像库Android-Universal-Image-Loader或Volley等

答案 1 :(得分:0)

尝试按以下方式对您的布局进行充气:

   if (convertView == null) 
 {
convertView = l_Inflater.inflate(R.layout.gridlayout_item, null);
holder = new ViewHolder();
holder.imageView = (ImageView) convertView
    .findViewById(R.id.iv_item_image);
convertView.setTag(holder);
 } 
  else {
         holder = (ViewHolder) convertView.getTag();
    }   
 static class ViewHolder{
        ImageView imageView; 
     }

这是解决滚动问题的最佳解决方案。

我希望这会对你有所帮助。