2012-12-07 80 views
0

在我的应用程序中,我有一个列表视图。每行包含一个图像和相对布局的textview ..一切都完成了..但是listview没有像原生的“联系人列表”滚动那么平滑。我认为问题在于getview(),因为在这种方法中我有一个“HTTP”调用和位图图像加载..是否有无论如何这样做..请帮助我..提前感谢..在列表视图中平滑滚动

我的Getview方法:

public View getView(int position, View convertView, ViewGroup parent) 
    { 
     // TODO Auto-generated method stub  
     Bitmap bitmap = DownloadImage(
       kickerimage[position]);   
//  View listView = convertView; 
     ViewHolder holder; 
     if (convertView == null) 
      { 
       //this should only ever run if you do not get a view back    
      LayoutInflater inflater = (LayoutInflater) contxt 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.homelistrow, null); 

      holder = new ViewHolder(); 

      holder.image = (ImageView) convertView 
         .findViewById(R.id.icon); 
      holder.image.setImageBitmap(bitmap); 

      holder.text = (TextView) convertView 
         .findViewById(R.id.name_label); 
      holder.text.setText(itemsarray[position]); 
      } 
    return convertView ;   
    } 

private Bitmap DownloadImage(String URL) 
    {   
//  System.out.println("image inside="+URL); 
     Bitmap bitmap = null; 
     InputStream in = null;   
     try { 
      in = OpenHttpConnection(URL); 
      bitmap = BitmapFactory.decodeStream(in); 
      in.close(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
//  System.out.println("image last"); 
     return bitmap;     
    } 
    private InputStream OpenHttpConnection(String urlString) 
      throws IOException 
      { 
       InputStream in = null; 
       int response = -1; 

       URL url = new URL(urlString); 
       URLConnection conn = url.openConnection(); 

       if (!(conn instanceof HttpURLConnection))      
        throw new IOException("Not an HTTP connection"); 

       try{ 
        HttpURLConnection httpConn = (HttpURLConnection) conn; 
        httpConn.setAllowUserInteraction(false); 
        httpConn.setInstanceFollowRedirects(true); 
        httpConn.setRequestMethod("GET"); 
        httpConn.connect(); 

        response = httpConn.getResponseCode();     
        if (response == HttpURLConnection.HTTP_OK) 
        { 
         in = httpConn.getInputStream();         
        }      
       } 
       catch (Exception ex) 
       { 
        throw new IOException("Error connecting");    
       } 
       return in;  
    } 

回答

2

从您的代码中,我看到您正在同一线程中的getView()函数内下载图像。使用webImageView来执行此任务而不是本地ImageView

这里是样品

android-remoteimageview

+0

感谢阿里·伊姆兰...ü可以张贴一些链接做webImageView .. – Subburaj

+0

她的链接为我t:-https://github.com/tom-dignan/android-remoteimageview –

+0

谢谢阿里伊姆兰..我会为此尝试,.. .. – Subburaj

1

为什么你ListView不滚动顺利,是因为你是从上mainthread的InputStream获取图像的原因。这意味着,无论何时你想加载一个图片,主线程都会被延迟,并且你的ListView

解决方法是将图像加载到不同的线程上,然后是主线程。这称为图像的延迟加载,并通过启动一个新线程(主要通过AsyncTask完成)来加载图像来完成。

这里有您可以访问的例子一些相关链接:

http://andytsui.wordpress.com/2012/05/10/tutorial-lazy-loading-list-views-in-android-binding-44/

http://codehenge.net/blog/2011/06/android-development-tutorial-asynchronous-lazy-loading-and-caching-of-listview-images/

http://thinkandroid.wordpress.com/2012/06/13/lazy-loading-images-from-urls-to-listviews/

https://github.com/commonsguy/cwac-endless