2012-12-07 179 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