package zss.bwei.com.weektwo2;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.bwei.imageloaderlibrary.utils.ImageLoaderUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
List<String> list=new ArrayList<>();
List<ImageView> listimage=new ArrayList<>();
private ViewPager viewPager;
private LinearLayout linerlayout;
//线程进行自动轮播
public Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
int currentItem = viewPager.getCurrentItem();
//切换至下一个页面
viewPager.setCurrentItem(++currentItem);
//再次调用
handler.sendEmptyMessageDelayed(1, 2000);
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewpager);
linerlayout = (LinearLayout) findViewById(R.id.linearLayout);
initData();
//添加小圆点
addPoint();
//适配数据
spadapter();
//进行无线轮播
viewPager.setCurrentItem(list.size()*1000);
//viewpager的点击监听
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
for (int i = 0; i <list.size(); i++) {
//选中的页面改变小圆点为选中状态,反之为未选中
if ((position %list.size()) == i) {
((View)listimage.get(i)).setBackgroundResource(R.drawable.dot_normal);
} else {
((View)listimage.get(i)).setBackgroundResource(R.drawable.dot_focus);
}
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
handler.sendEmptyMessageDelayed(1, 2000);
}
private void addPoint() {
for (int i = 0; i <list.size(); i++) {
ImageView imageView1 = new ImageView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//为小圆点左右添加间距
params.leftMargin = 10;
params.rightMargin = 10;
//手动给小圆点一个大小
params.height =15;
params.width =15;
if (i == 0) {
imageView1.setBackgroundResource(R.drawable.dot_normal);
} else {
imageView1.setBackgroundResource(R.drawable.dot_focus);
}
//为LinearLayout添加ImageView
linerlayout.addView(imageView1, params);
listimage.add(imageView1);
}
}
private void spadapter() {
MyAdapter myadapter=new MyAdapter();
viewPager.setAdapter(myadapter);
}
private void initData() {
list.add("http://l2.51fanli.net//tuan//images//1//5806eac956808.jpg");
list.add("http://l2.51fanli.net//tuan//images//b//580991bb30560.jpg");
list.add("http://l0.51fanli.net//tuan//images//b//58115f2593dc3.jpg");
list.add("http://l2.51fanli.net//tuan//images//0//57923840b054d.jpg");
list.add("http://l2.51fanli.net//tuan//images//e//58101e11ab164.jpg");
}
private class MyAdapter extends PagerAdapter{
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
position=position%list.size();
ImageView imageview = new ImageView(MainActivity.this);
DisplayImageOptions options = ImageLoaderUtils.getOptions();
ImageLoader.getInstance().displayImage(list.get(position),imageview,options);
container.addView(imageview);
return imageview;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
viewPager.removeView((View) object);
}
}
}
//布局