//adapter
public class adapter extends PagerAdapter{
private Context context;
private List<ImageView> list;
public adapter(Context context, List<ImageView> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = list.get(position % list.size());
ViewParent group = imageView.getParent();
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
///app
public class imageloader extends Application{
@Override
public void onCreate() {
super.onCreate();
ImageLoaderConfiguration configuration = ImageLoaderUtils.getConfiguration(this);
ImageLoader.getInstance().init(configuration);
}
}
//zhuye
public class MainActivity extends AppCompatActivity {
private String spec="http://172.17.8.100/mobile/exam/findCommodityList";
private ViewPager view_page;
private List<ImageView> list = new ArrayList<>();
private Handler handler= new Handler(){
@Override
public void handleMessage(Message msg) {
if (msg.what==0){
String json = (String) msg.obj;
Log.i("xxx",json+"");
Gson gson = new Gson();
news news = gson.fromJson(json, news.class);
com.example.bean.news.ResultBean result = news.getResult();
List<com.example.bean.news.ResultBean.PicsBean> pics = result.getPics();
for (int i = 0; i < pics.size(); i++) {
String pic = pics.get(i).getPic();
ImageView imageView = new ImageView(MainActivity.this);
list.add(imageView);
ImageLoader.getInstance().displayImage(pic,imageView);
adapter adapter = new adapter(MainActivity.this,list);
view_page.setAdapter(adapter);
}
}else if(msg.what==1){
int i = view_page.getCurrentItem();
i++;
view_page.setCurrentItem(i);
handler.sendEmptyMessageDelayed(1,1000);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view_page = findViewById(R.id.view_page);
jiexi(spec);
}
private void jiexi(final String spec) {
new Thread(){
@Override
public void run() {
try {
URL url = new URL(spec);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.setRequestMethod("GET");
if(connection.getResponseCode()==200){
InputStream inputStream = connection.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int len = 0 ;
while ((len=inputStream.read(bytes))!=-1){
bos.write(bytes,0,len);
}
inputStream.close();
bos.close();
String json = bos.toString();
Message message = new Message();
message.what=0;
message.obj=json;
handler.sendMessage(message);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
//得到焦点
@Override
protected void onResume() {
super.onResume();
handler.sendEmptyMessageDelayed(1,1000);
}
//失去焦点
@Override
protected void onPause() {
super.onPause();
handler.removeCallbacksAndMessages(null);
}
}