import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity {
private ViewPager viewPager;
private LinearLayout linearLayout;
private List<ImageView> listImage = new ArrayList<ImageView>();
private List<ImageView> listPoint = new ArrayList<ImageView>();
private String [] images = {"https://img13.360buyimg.com/da/jfs/t4915/282/2020228257/154078/9e4cf39d/58f7313eNb38e5b38.jpg" ,
"https://img1.360buyimg.com/da/jfs/t5023/170/1892599860/100393/3943e3ed/58f5d02fN9ff3d04b.jpg" ,
"https://img1.360buyimg.com/da/jfs/t4579/205/2161686308/43529/68904060/58ec8c7fN581b2e1f.jpg"} ;
IPagerAdapter adapter ;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 1 :
int count = viewPager.getCurrentItem() + 1;
viewPager.setCurrentItem(count);
change();
break;
}
}
} ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewpager);
linearLayout = (LinearLayout) findViewById(R.id.dian_linear);
createImageView();
createPoint();
adapter = new IPagerAdapter(images,this);
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
for(int i=0;i<listPoint.size() ; i++){
if(i == position % listPoint.size()){
listPoint.get(i).setImageResource(R.drawable.oval_normal);
} else {
listPoint.get(i).setImageResource(R.drawable.oval_selected);
}
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
viewPager.setCurrentItem(listImage.size() * 100);
change();
}
private void createPoint() {
for (int i = 0; i < images.length; i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
ImageView imageView = new ImageView(this);
params.setMargins(0,0,8,0);
imageView.setLayoutParams(params);
listPoint.add(imageView);
if(i == 0){
imageView.setImageResource(R.drawable.oval_normal);
} else {
imageView.setImageResource(R.drawable.oval_selected);
}
linearLayout.addView(imageView);
}
}
private void change(){
handler.sendEmptyMessageDelayed(1,2000);
}
private void createImageView(){
for (int i= 0 ;i < images.length ; i++) {
ImageView imageView = new ImageView(this);
listImage.add(imageView);
ImageLoader.getInstance().displayImage(images[i],imageView);
}
}
}
package com.example.muhanxi.viewpagerdemo;
import android.content.Context;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.List;
/**
* Created by muhanxi on 17/4/21.
*/
public class IPagerAdapter extends PagerAdapter {
private String [] list ;
private Context context ;
private Handler handler ;
public IPagerAdapter(String [] strings, Context context){
this.list = strings;
this.context = context;
}
@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) {
ImageView imageView = new ImageView(context);
imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 取消掉handler 延时消息
// handler.removeCallbacksAndMessages(null);
break;
case MotionEvent.ACTION_UP:
// 开启延迟发送 消息
// handler.sendEmptyMessageDelayed(1,2000);
break;
}
return true;
}
});
ImageLoader.getInstance().displayImage(list[position % list.length] , imageView);
container.addView(imageView);
return imageView ;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}