0

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

我已经提到了gridview中的Images 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);
}
}
4

2 回答 2

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; 
     }

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

我希望这能帮到您。

于 2013-08-09T12:50:17.943 回答
0

当你滚动你的剂量时,不要取消你以前的图像加载。
首先,阅读 android教程以了解这种情况。
二、看使用图片库Android-Universal-Image-Loader或者 Volley 等。

于 2013-08-08T18:09:18.063 回答