布局里面
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/view_pager"
></android.support.v4.view.ViewPager>
</RelativeLayout>
MainActivity
public class MainActivity extends AppCompatActivity {
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if (msg.what==0){
int currentItem = viewPager.getCurrentItem();
viewPager.setCurrentItem(currentItem+1);
handler.sendEmptyMessageDelayed(0,2000);
}
}
};
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.view_pager);
List<String> listlunbo = new ArrayList<>();
listlunbo.add("http://pic8.nipic.com/20100701/5290458_114840036316_2.jpg");
listlunbo.add("http://pic2.nipic.com/20090424/1468853_230119053_2.jpg");
listlunbo.add("http://img3.3lian.com/2013/s1/20/d/57.jpg");
listlunbo.add("http://pic39.nipic.com/20140226/18071023_164300608000_2.jpg");
listlunbo.add("http://a0.att.hudong.com/15/08/300218769736132194086202411_950.jpg");
LunBoAdapter lunBoAdapter = new LunBoAdapter(listlunbo, MainActivity.this,handler);
viewPager.setAdapter(lunBoAdapter);
handler.sendEmptyMessageDelayed(0,2000);
}
}
轮播图的适配器 继承pageradapter 轮播图触摸的事件 跳转
public class LunBoAdapter extends PagerAdapter{
List<String> listlunbo;
Context context;
Handler handler;
public LunBoAdapter(List<String> listlunbo, Context context, Handler handler) {
this.listlunbo = listlunbo;
this.context = context;
this.handler = handler;
}
@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.setScaleType(ImageView.ScaleType.FIT_XY);
ImageLoader.getInstance().displayImage(listlunbo.get(position%listlunbo.size()),imageView,ImageLoaderUtil.getDefautOption());
imageView.setOnTouchListener(new View.OnTouchListener() {
boolean tiaozhuan = true;
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
handler.removeCallbacksAndMessages(null);
break;
case MotionEvent.ACTION_MOVE:
tiaozhuan = false;
handler.removeCallbacksAndMessages(null);
break;
case MotionEvent.ACTION_CANCEL:
tiaozhuan = false;
handler.sendEmptyMessageDelayed(0,2000);
break;
case MotionEvent.ACTION_UP:
if (tiaozhuan){
//如果是点击的图片的话,没有移动和取消
Intent intent = new Intent(context, SecondActivity.class);
context.startActivity(intent);
((MainActivity)context).finish();
}
handler.sendEmptyMessageDelayed(0,2000);
break;
}
return true;
}
});
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}}