package com.liulailin.day12_viewpager;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.bwei.adapter.MyAdapter;
import com.bwei.bean.DataBean;
import com.com.bwei.utils.NetWorkUtils;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.V;
public class MainActivity extends AppCompatActivity {
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int mum = msg.what;
switch (mum){
case 0:
String json = (String) msg.obj;
GsonData(json);
// Toast.makeText(MainActivity.this,json,Toast.LENGTH_LONG).show();
break;
case 1:
//得到当前viewpager的索引
int position = vp.getCurrentItem();
//让当前的的viewpager加1
position++;
//给viewPager设置当前的索引
vp.setCurrentItem(position);
sendMsg();
break;
}
}
};
private ViewPager vp;
private LinearLayout ll;
private List<DataBean.Adlist> adlist;
private ImageView iv;
private List<ImageView> ivList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(){
@Override
public void run() {
super.run();
String json = NetWorkUtils.getjson();
Message msg = new Message();
msg.obj =json;
msg.what=0;
handler.sendMessage(msg);
}
}.start();
vp = (ViewPager) findViewById(R.id.vp);
ll = (LinearLayout) findViewById(R.id.ll);
}
/**
* 解析json字符串
*/
private void GsonData(String json) {
Gson gson = new Gson();
DataBean dataBean = gson.fromJson(json, DataBean.class);
//得到所有的数据
adlist = dataBean.data.adlist;
//把数据传到adapter里面
MyAdapter adapter = new MyAdapter(adlist,MainActivity.this);
vp.setAdapter(adapter);
//初使化点
initDot();
//让viewpager的索引默认的指在100000
vp.setCurrentItem(100000);
//自动轮博
sendMsg();
//viewPager的监听事件
ViewPagerListener();
}
/**
* viewpager的滑动监听事件
*/
private void ViewPagerListener() {
vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
for (int x=0;x<ivList.size();x++){
if(position%adlist.size()==x){
//证明点和图片是对应的,应该点是变红的
ivList.get(position%adlist.size()).setImageResource(R.drawable.dot_focuable);
}else{
ivList.get(x).setImageResource(R.drawable.dot_normal);
}
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
/**
* 初使化点的
*/
private void initDot() {
ivList = new ArrayList<>();
if(ivList !=null){
ivList.clear();
}
for (int x =0;x<adlist.size();x++){
iv = new ImageView(MainActivity.this);
if (x==0){
//第一张,让其圆点进行选中状态
iv.setImageResource(R.drawable.dot_focuable);
}else{
iv.setImageResource(R.drawable.dot_normal);
//其它的都不选中
}
LinearLayout.LayoutParams parmes = new LinearLayout.LayoutParams(20,20);
//设置两个点之间的距离
parmes.setMargins(10,0,10,0);
ll.addView(iv,parmes);
ivList.add(iv);
}
}
/*8
实现 自动轮博
*/
private void sendMsg() {
handler.sendEmptyMessageDelayed(1,2000);
}
}