在listview中平滑滚动

时间:2012-12-07 07:48:38

标签: android listview

在我的应用程序中,我有一个listview ..每行包含一个图像和相对布局的textview。一切都完成了。但是listview不是那么平滑地滚动为本机“联系人列表”。我认为问题位于getview(),y因为在这个方法中我有一个“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 个答案:

答案 0 :(得分:2)

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

这是样本

android-remoteimageview

答案 1 :(得分:1)

您的ListView未顺畅滚动的原因是您从主线程上的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