XListView 다중 항목 로드 ImageLoader 구현

6555 단어
private Handler mHandler=new Handler(){ @Override public void handleMessage(Message msg) { if(msg.what==1){ if(page==1) mList.clear(); page++;
                  Data data= (Data) msg.obj;
                  mList.addAll(data.getData());
                  mAdapter.notifyDataSetChanged();

                  stopLoad();
              }
          }
      };
private List mList;
private XListView mXListView;
private MyAdapter mAdapter;
private int page = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initView();

    loadData();
}

private void initView() {
    mList=new ArrayList<>();
    mXListView=(XListView)findViewById(R.id.xlistview);
    mXListView.setPullLoadEnable(true);
    mXListView.setXListViewListener(this);
    mAdapter=new MyAdapter();

    mXListView.setAdapter(mAdapter);
}

/**
 *  
 */
private void loadData() {
    new Thread(){
        @Override
        public void run() {
            String urlPath = "http://ic.snssdk.com/2/article/v25/stream/?category=news_tech&count=20&bd_latitude=4.9E-324&bd_longitude=4.9E-324&bd_loc_time=1457695555&loc_mode=5&lac=4527&cid=28883&iid=3835029558&device_id=12211880440&ac=wifi&channel=baidu&aid=13&app_name=news_article&version_code=460&device_platform=android&device_type=iToolsAVM&os_api="+page;
            try {
                URL url =new URL(urlPath);
                HttpURLConnection urlConnection=(HttpURLConnection)url.openConnection();
                urlConnection.connect();

                if(urlConnection.getResponseCode()==200){
                    InputStream in = urlConnection.getInputStream();
                    BufferedReader br = new BufferedReader(new InputStreamReader(in));
                    StringBuffer sb=new StringBuffer();
                    String line = null;
                    while((line=br.readLine())!=null){
                        sb.append(line);
                    }
                    Log.e("loadData", "loadData:"+sb.toString() );
                    KLog.json(sb.toString());

                    Gson gson=new Gson();
                    Data data= gson.fromJson(sb.toString(),Data.class);

                    Message msg=Message.obtain();
                    msg.what=1;
                    msg.obj=data;
                    mHandler.sendMessage(msg);
                }else{
                    Log.e("loadData", "loadData: ---"+urlConnection.getResponseCode());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }.start();
}

@Override
public void onRefresh() {
    page = 1;
    loadData();
}

@Override
public void onLoadMore() {
    loadData();
}

private void stopLoad(){
    mXListView.stopRefresh();
    mXListView.stopLoadMore();
    mXListView.setRefreshTime(" ");
}

class MyAdapter extends BaseAdapter{

    final  int TYPE0 = 0;
    final  int TYPE1 = 1;
    final  int TYPE3 = 2;
    ImageLoader imageLoader;
    DisplayImageOptions options;

    public MyAdapter() {
        imageLoader=ImageLoader.getInstance();
        ImageLoaderConfiguration config = ImageLoaderConfiguration.createDefault(MainActivity.this);
        imageLoader.init(config);

        options=new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.ic_launcher)
                .build();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int size) {
        switch (size){
            case 0:
                return TYPE0;
            case 1:
                return TYPE1;
            case 3:
                return TYPE3;
            default:
                return TYPE0;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Data.DataBean bean= mList.get(position);
        List listImages =bean.getImage_list();
        int size = listImages!=null?bean.getImage_list().size():0;
        int type = getItemViewType(size);

        KLog.e(position+"/"+size+"/"+type);
        ViewHolder holder=null;
        convertView=null;
        if(convertView==null){
            holder=new ViewHolder();
            switch (type){
                case TYPE0:
                    convertView=View.inflate(MainActivity.this,R.layout.item0,null);
                    break;
                case TYPE1:
                    convertView=View.inflate(MainActivity.this,R.layout.item1,null);
                    holder.image1=(ImageView)convertView.findViewById(R.id.image1);
                    break;
                case TYPE3:
                    convertView=View.inflate(MainActivity.this,R.layout.item3,null);
                    holder.image1=(ImageView)convertView.findViewById(R.id.image1);
                    holder.image2=(ImageView)convertView.findViewById(R.id.image2);
                    holder.image3=(ImageView)convertView.findViewById(R.id.image3);
                    break;
            }
            holder.title=(TextView)convertView.findViewById(R.id.title);
            holder.source=(TextView)convertView.findViewById(R.id.source);

            convertView.setTag(holder);
        }else{
            holder=(ViewHolder)convertView.getTag();
        }

        holder.title.setText(bean.getTitle()+"-"+size);
        holder.source.setText(bean.getSource());


        switch (type){
            case TYPE1:
                imageLoader.displayImage(listImages.get(0).getUrl(),holder.image1,options);
                break;
            case TYPE3:
                imageLoader.displayImage(listImages.get(0).getUrl(),holder.image1,options);
                imageLoader.displayImage(listImages.get(1).getUrl(),holder.image2,options);
                imageLoader.displayImage(listImages.get(2).getUrl(),holder.image3,options);
                break;
        }
        return convertView;
    }
}

class ViewHolder{
    TextView title,source;
    ImageView image1,image2,image3;
}

좋은 웹페이지 즐겨찾기