android listview viewpager 异步加载图片 headerview

来源:互联网 发布:越狱后抹除数据白苹果 编辑:程序博客网 时间:2024/05/17 02:54

//这是代码下载地址:http://download.csdn.net/detail/crazy_zhangqiang/4612674




package me.maxwin;


import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import java.util.List;




import me.maxwin.AsyncImageLoader.ImageCallback;
import me.maxwin.view.XListView;
import me.maxwin.view.XListView.IXListViewListener;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class XListViewActivity extends Activity implements IXListViewListener {
    private XListView mListView;
    private MyAdapter mAdapter;
    private ArrayList<String> items = new ArrayList<String>();
    private Handler mHandler;
    private int start = 0;
    private static int refreshCnt = 0;
    
    //viewpager
    private ViewPager viewPager;
    private ArrayList<View> pageViews;
    private ViewGroup group;
    private ImageView imageView;
    private ImageView[] imageViews;
    private OnClickListener listener;
    private List<ImageView>imageViewsInPageView;
    
    
    //图片异步加载
    private AsyncImageLoader asyncImageLoader;
    //记录图片的url地址
    List<String> mListStrings = null;

    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        geneItems();
        mListView = (XListView) findViewById(R.id.xListView);
        this.mListView.addHeaderView(this.createHeader());

        mListView.setPullLoadEnable(true);
//        mListView.setPullLoadEnable(false);
//        mListView.setPullRefreshEnable(false);
        mListView.setXListViewListener(this);
        this.setandUpdateListView();

        mHandler = new Handler();
    }
    
      public void setandUpdateListView(){
            
            
            if(this.mAdapter==null){
                
                this.mListStrings = new ArrayList<String>();  
                addurl();  
                
                this.mAdapter = new MyAdapter(this, this.mListStrings,this.mListView);  
                mListView.setAdapter(mAdapter);

            }else{
                
                
            }
            
        }
        
        public void addurl() {  
            mListStrings.add("http://images.zhangsou.cn/mycms/uploadfile/clients/2012/0511/13367174844275.jpg");  
            mListStrings.add("http://images.zhangsou.cn/mycms/uploadfile/clients/2012/0502/13359385012108.jpg");  
            mListStrings.add("http://images.zhangsou.cn/mycms/uploadfile/clients/2012/0502/13359376797926.jpg");  
            mListStrings.add("http://images.zhangsou.cn/mycms/uploadfile/clients/2012/0502/13359375086843.jpg");  
            mListStrings.add("http://images.zhangsou.cn/mycms/uploadfile/clients/2012/0502/13359372955919.jpg");  
            mListStrings.add("http://images.zhangsou.cn/mycms/uploadfile/clients/2012/0502/13359371327848.jpg");  
            mListStrings.add("ttp://images.zhangsou.cn/mycms/uploadfile/clients/2012/0502/13359369967997.jpg");  
            mListStrings.add("http://images.zhangsou.cn/mycms/uploadfile/clients/2011/1219/13242668627775.jpg");  
            mListStrings.add("http://images.zhangsou.cn/mycms/uploadfile/clients/2012/0502/13359371327848.jpg");  
            mListStrings.add("http://images.zhangsou.cn/mycms/uploadfile/clients/2011/1219/13242669433396.jpg");  
            mListStrings.add("http://images.zhangsou.cn/mycms/uploadfile/clients/2011/1219/13242766532906.jpg");  
        }  
        
    

    private void geneItems() {
        for (int i = 0; i != 20; ++i) {
            items.add("refresh cnt " + (++start));
        }
    }

    private void onLoad() {
        mListView.stopRefresh();
        mListView.stopLoadMore();
        mListView.setRefreshTime("鍒氬垰");
    }
    
    public void onRefresh() {
        mHandler.postDelayed(new Runnable() {
            public void run() {
                start = ++refreshCnt;
                items.clear();
                geneItems();
                // mAdapter.notifyDataSetChanged();
                if(mAdapter==null)
                {
                    mAdapter = new MyAdapter(XListViewActivity.this, mListStrings,mListView);
                }else
                {
                    mAdapter.notifyDataSetChanged();
                }
                mListView.setAdapter(mAdapter);
                onLoad();
            }
        }, 2000);
    }

    public void onLoadMore() {
        mHandler.postDelayed(new Runnable() {
            public void run() {
                //geneItems();
                mAdapter.notifyDataSetChanged();
                onLoad();
            }
        }, 2000);
    }

    private View createHeader() {

        View v = this.getLayoutInflater().inflate(R.layout.list_header_item,
                null);
         this.listener  = new OnClickListener(){

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    int imageId = v.getId();
                    switch(imageId)
                    {
                    case me.maxwin.R.id.item01_imageview:
                        for(int i=0;i<imageViewsInPageView.size();i++)
                        {
                            imageViewsInPageView.get(i).setBackgroundResource(me.maxwin.R.drawable.ic_launcher);
                        }
                        break;
                    case me.maxwin.R.id.item02_imageview:
                        break;
                    case me.maxwin.R.id.item03_imageview:
                        break;
                    case me.maxwin.R.id.item04_imageview:
                        break;
                    
                    }
                    Toast.makeText(getApplicationContext(), ""+imageId, Toast.LENGTH_LONG).show();
                }
            };
            LayoutInflater inflater = this.getLayoutInflater();
            
            this.imageViewsInPageView =new ArrayList<ImageView>();
            
            this.pageViews = new ArrayList<View>();
            this.pageViews.add(inflater.inflate(R.layout.item01, null));
            this.pageViews.add(inflater.inflate(R.layout.item02, null));
            this.pageViews.add(inflater.inflate(R.layout.item03, null));
            this.pageViews.add(inflater.inflate(R.layout.item04, null));
            
            this.imageViews = new ImageView[this.pageViews.size()];

         // group是R.layou.main中的负责包裹小圆点的LinearLayout.    
            group = (ViewGroup)v.findViewById(R.id.viewGroup);
            this.viewPager = (ViewPager)v.findViewById(R.id.guidePages);
            
            for(int i=0;i<this.pageViews.size();i++)
            {
                imageView  = new ImageView(this);
                imageView.setLayoutParams(new LayoutParams(20,20));
                imageView.setPadding(20, 0, 20, 20);
                this.imageViews[i]=imageView;
                if(i==0)
                {
                    //默认选中第一个view
                    this.imageViews[i].setBackgroundResource(R.drawable.dot_selected);
                }else{
                    
                    
                    this.imageViews[i].setBackgroundResource(R.drawable.dot_none);
                }
                group.addView(this.imageViews[i]);
            }
            viewPager.setAdapter(new GuidePageAdapter());
            viewPager.setOnPageChangeListener(new GuidePageChangeListener());  
          
            this.pageViews.get(0).findViewById(R.id.item01_imageview).setOnClickListener(listener);
            this.pageViews.get(1).findViewById(R.id.item02_imageview).setOnClickListener(listener);
            this.pageViews.get(2).findViewById(R.id.item03_imageview).setOnClickListener(listener);
            this.pageViews.get(3).findViewById(R.id.item04_imageview).setOnClickListener(listener);
            this.imageViewsInPageView.add((ImageView) this.pageViews.get(0).findViewById(R.id.item01_imageview));
            this.imageViewsInPageView.add((ImageView) this.pageViews.get(1).findViewById(R.id.item02_imageview));
            this.imageViewsInPageView.add((ImageView) this.pageViews.get(2).findViewById(R.id.item03_imageview));
            this.imageViewsInPageView.add((ImageView) this.pageViews.get(3).findViewById(R.id.item04_imageview));


        return v;

    }
      class GuidePageAdapter extends PagerAdapter{

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return pageViews.size();
            }

            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                // TODO Auto-generated method stub
                return arg0==arg1;
            }
            
            public int getItemPosition(Object object) {
                // TODO Auto-generated method stub
                return super.getItemPosition(object);
            }

            public void destroyItem(View arg0, int arg1, Object arg2) {
                // TODO Auto-generated method stub
                ((ViewPager) arg0).removeView(pageViews.get(arg1));
            }

            public Object instantiateItem(View arg0, int arg1) {
                // TODO Auto-generated method stub
                ((ViewPager) arg0).addView(pageViews.get(arg1));
                return pageViews.get(arg1);
            }
            
            public void restoreState(Parcelable arg0, ClassLoader arg1) {
                // TODO Auto-generated method stub

            }

            public Parcelable saveState() {
                // TODO Auto-generated method stub
                return null;
            }
            public void startUpdate(View arg0) {    
                // TODO Auto-generated method stub    
        
            }    
        
            @Override    
            public void finishUpdate(View arg0) {    
                // TODO Auto-generated method stub    
        
            }    
        }
        class GuidePageChangeListener implements OnPageChangeListener
        {

            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub
                
            }

            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub
                
            }

            public void onPageSelected(int arg0) {
                // TODO Auto-generated method stub
                  for (int i = 0; i < imageViews.length; i++) {    
                        imageViews[arg0]    
                                .setBackgroundResource(R.drawable.dot_selected);    
                        if (arg0 != i) {    
                            imageViews[i]    
                                    .setBackgroundResource(R.drawable.dot_none);    
                        }    
                    }  
            }
        }
        
    
        
        //MyAdpater
        
        class MyAdapter extends BaseAdapter{

              
            List<String> mData;  
            Context mContext;  
            ImageView mImageView;  
            TextView mTextView;
             private ListView mListView;  
             public MyAdapter(Context context, List<String> list,ListView listView) {  
                 this.mContext = context;  
                 this.mData = list;  
                 asyncImageLoader = new AsyncImageLoader();  
                 this.mListView=listView;
                 // TODO Auto-generated constructor stub  
             }  
             
             
            
            public int getCount() {
                // TODO Auto-generated method stub
                return mData.size();
            }

            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return mData.get(position);
            }

            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return position;
            }

            public View getView(int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                if (convertView == null) {  
                    convertView = LayoutInflater.from(mContext).inflate(R.layout.item,  
                            null);  
                }
                
                mImageView = (ImageView) convertView.findViewById(R.id.imageView1);
                String url=mData.get(position).toString();  
                mImageView.setTag(url);
                this.mTextView =(TextView)convertView.findViewById(R.id.textView);
                this.mTextView.setText(mListStrings.get(position));
                
                
                Drawable cachedImage = asyncImageLoader.loadDrawable(url, new ImageCallback() {  
                    public void imageLoaded(Drawable imageDrawable, String imageUrl) {  
                        ImageView imageViewByTag = (ImageView) mListView.findViewWithTag(imageUrl);  
                        if (imageViewByTag != null) {  
                            imageViewByTag.setImageDrawable(imageDrawable);  
                        }  
                    }  
                });  
                if (cachedImage == null) {  
                    this.mImageView.setImageResource(R.drawable.ic_launcher);  
                }else{  
                    this.mImageView.setImageDrawable(cachedImage);  
                }  
               
                return convertView;  
            }
            
            
            
        }
        
        
        
}

class AsyncImageLoader{
    
    private HashMap<String, SoftReference<Drawable>> imageCache;  
    public AsyncImageLoader() {  
        imageCache = new HashMap<String, SoftReference<Drawable>>();  
    }  
   
    
    public interface ImageCallback {  
        public void imageLoaded(Drawable imageDrawable, String imageUrl);  
    }  
    public Drawable loadDrawable(final String imageUrl, final ImageCallback imageCallback) {  
        if (imageCache.containsKey(imageUrl)) {  
            SoftReference<Drawable> softReference = imageCache.get(imageUrl);  
            Drawable drawable = softReference.get();  
            if (drawable != null) {  
                return drawable;  
            }  
        }  
        final Handler handler = new Handler() {  
            public void handleMessage(Message message) {  
                imageCallback.imageLoaded((Drawable) message.obj, imageUrl);  
            }  
        };  
        new Thread() {  
            @Override  
            public void run() {  
                Drawable drawable = loadImageFromUrl(imageUrl);  
                imageCache.put(imageUrl, new SoftReference<Drawable>(drawable));  
                Message message = handler.obtainMessage(0, drawable);  
                handler.sendMessage(message);  
            }  
        }.start();  
        return null;  
    }
    
    
    public static Drawable loadImageFromUrl(String url) {  
        URL m;  
        InputStream i = null;  
        try {  
            m = new URL(url);  
            i = (InputStream) m.getContent();  
        } catch (MalformedURLException e1) {  
            e1.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        Drawable d = Drawable.createFromStream(i, "src");  
        return d;  
    }
    public void clearCache(){
       
        if(this.imageCache.size()>0){
           
            this.imageCache.clear();
        }
       
    }
}



原创粉丝点击