package com.baway.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import com.baway.bean.Conn;
import com.baway.bean.Data;
import com.baway.bean.Like_user_list;
import com.google.gson.Gson;
import com.lidroid.xutils.BitmapUtils;
public class IndexActivity extends Activity {
private Intent in;
private int p;
private String uri="http://mbfun.funwear.com/mbfun_server/index.php?m=Home&a=getCollocationListHot&page=1";
private String s;
private InputStream ins;
private List<Data> data;
private ArrayList<Like_user_list> list;
private ViewPager vp;
private LinearLayout dotcontaint;
ArrayList<ImageView> imgs ;
ArrayList<View> dots ;
int currentIndex = 0;
int olderIndex = 0;
Handler h = new Handler(){
public void handleMessage(android.os.Message msg) {
vp.setCurrentItem(currentIndex);//设置此次要显示的pager
//切换选中的圆点
dots.get(olderIndex).setBackgroundResource(R.drawable.dot_nomal);//设置上次选中的圆点为不选中
dots.get(currentIndex%list.size()).setBackgroundResource(R.drawable.dot_focus);//设置当前选中的圆点为选中
olderIndex = currentIndex%list.size();
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_index);
vp = (ViewPager) findViewById(R.id.vp);
dotcontaint = (LinearLayout) findViewById(R.id.dotcontaint);
in=getIntent();
p=in.getIntExtra("p",100);
s=in.getStringExtra("s");
init1();
init();
}
private void init1() {
Gson g=new Gson();
Conn c=g.fromJson(s,Conn.class);
data=c.getData();
list=data.get(p).getLike_user_list();
}
public void init(){
//获得网络图片,配置给Veiwpager
getImageViewList();
//获得圆点
getDotList();
//设置第一个圆点为选中状态
dots.get(0).setBackgroundResource(R.drawable.dot_focus);
vp.setAdapter(new MyVpAdapger());//配置pager页
int currentItem = Integer.MAX_VALUE/2-Integer.MAX_VALUE/2%list.size();
vp.setCurrentItem(currentItem);
//通过定时器,制作自动划动效果
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
currentIndex = vp.getCurrentItem()+1;//下一个页
h.sendEmptyMessage(0x123);//在此线程中,不能操作ui主线程
}
}, 3000, 2000);
}
private void getDotList() {
dots = new ArrayList<View>();
//循环图片数组,创建对应数量的dot
for(int i=0;i<list.size();i++){
View view = LayoutInflater.from(this).inflate(R.layout.dot_layout, null);//加载布局
View dot = view.findViewById(R.id.dotView);//得到布局中的dot点组件
//收集dot
dots.add(dot);
//把布局添加到线性布局
dotcontaint.addView(view);
}
}
class MyVpAdapger extends PagerAdapter{
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0==arg1;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// super.destroyItem(container, position, object);
// ImageView img = imgs.get(position);
container.removeView((View)object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
int item = position%list.size();
container.addView(imgs.get(item));
return imgs.get(item);
}
}
/**
* 到网络获得图片信息,并赋值到ImageView中
*/
private void getImageViewList() {
imgs = new ArrayList<ImageView>();
BitmapUtils btUtil = new BitmapUtils(this);
//加载图片
for(int i=0;i<list.size();i++){
ImageView img = new ImageView(this);
img.setScaleType(ScaleType.FIT_XY);
btUtil.display(img,list.get(i).getHead_img());
imgs.add(img);
}
}
}