ImageLoader+lunbo+zk3
package com.baidu.imageloaderend;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import com.baidu.adaper.MyAdapter;
import com.baidu.adaper.MyLiAdapter;
import com.baidu.util.MyUtil;
import com.baidu.vo.SuperClass_2;
import com.baidu.vo.SuperClass_3;
import com.baidu.vo.Super_vp;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.BitmapUtils;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
public class MainActivity extends Activity {
// 访问路径
private static final String URL_1 = "http://172.17.29.120/localuser/ljy/jc/ad.json";
private static final String URL_2 = "http://172.17.29.120/localuser/ljy/jc/nav.json";
private static final String URL_3 = "http://172.17.29.120/localuser/ljy/jc/alluser.json";
private ViewPager viewPager;
private GridView gridView;
private ListView listView;
private List<ImageView> images;
private List<SuperClass_2> class_2;
private List<SuperClass_3> class_3;
int count = 0;
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
// 当有多个消息需要接收时需用what来判断执行效果
int i = msg.what;
if(i==1){
// 为图片设置适配器
setAdapter();
// 创建定时器
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
count++;
if(count==5){
count=0;
}
handler.sendEmptyMessage(4);
}
}, 1000, 2000);
}
if(i==2){
// 设置适配器
gridView.setAdapter(new MyAdapter(MainActivity.this, class_2));
}
if(i==4){
viewPager.setCurrentItem(count);
}
if(i==5){
listView.setAdapter(new MyLiAdapter(MainActivity.this, class_3));
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
// TODO Auto-generated method stub
// 找控件
findView();
// 请求轮播数据
requestImage();
// 解析json
getData_m();
// 为listview准备数据
getData_li();
}
private void getData_li() {
// TODO Auto-generated method stub
new Thread() {
public void run() {
String data_li = MyUtil.getJsonFromServer(URL_3);
Gson g = new Gson();
Type type = new TypeToken<List<SuperClass_3>>() {
}.getType();
class_3 = g.fromJson(data_li, type);
handler.sendEmptyMessage(5);
};
}.start();
}
private void getData_m() {
// TODO Auto-generated method stub
new Thread(){
public void run() {
String data_mi = MyUtil.getJsonFromServer(URL_2);
Gson g = new Gson();
Type type = new TypeToken<List<SuperClass_2>>() {
}.getType();
// Type type = new TypeToken<List<SuperClass_2>>() {
// }.getType();
class_2 = g.fromJson(data_mi, type);
System.out.println(class_2.get(0).getName());
handler.sendEmptyMessage(2);
};
}.start();
}
private void setAdapter() {
// TODO Auto-generated method stub
viewPager.setAdapter(new PagerAdapter() {
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0==arg1;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return images.size();
}
// 自己手动重写的方法destroyItem
@Override
public void destroyItem(ViewGroup container, int position,
Object object) {
// TODO Auto-generated method stub
// 注释掉父类的方法
// super.destroyItem(container, position, object);
container.removeView(images.get(position));
}
// instantiatitem初始化条目
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
container.addView(images.get(position));
return images.get(position);
}
});
};
private void requestImage() {
// TODO Auto-generated method stub
// 请求网路数据
new Thread() {
public void run() {
if (MyUtil.isNetAvailable(MainActivity.this)) {
String vp_data = MyUtil.getJsonFromServer(URL_1);
// 解析只有图片的json
Gson g = new Gson();
Type type = new TypeToken<List<Super_vp>>() {
}.getType();
List<Super_vp> list_vp = g.fromJson(vp_data, type);
// 循环为控件赋值
BitmapUtils bit = new BitmapUtils(MainActivity.this);
images = new ArrayList<ImageView>();
for (int i = 0; i < list_vp.size(); i++) {
ImageView image = new ImageView(MainActivity.this);
bit.display(image,
"http://172.17.29.120/localuser/ljy/jc/"
+ list_vp.get(i).getSrc());
images.add(image);
}
handler.sendEmptyMessage(1);
}
};
}.start();
}
private void findView() {
viewPager = (ViewPager) findViewById(R.id.vp);
gridView = (GridView) findViewById(R.id.gv);
listView = (ListView) findViewById(R.id.lv);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}