package com.example.viewpager; import java.util.ArrayList; import java.util.List; import java.util.Map; import android.app.Activity; import android.app.LocalActivityManager; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.os.Parcelable; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.view.Window; import android.widget.ImageView; import android.widget.LinearLayout; public class MainActivity extends Activity { private ViewPager viewPager; private List<View> list; private ImageView imageView; private ImageView[] imageViews; private int num = 300; private LocalActivityManager manager; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main_activity); manager = new LocalActivityManager(this , true); manager.dispatchCreate(savedInstanceState); list = new ArrayList<View>(); Intent intent = new Intent(MainActivity.this,ImageActivity1.class); list.add(getView("ImageActivity1", intent)); intent = new Intent(MainActivity.this,ImageActivity2.class); getView("ImageActivity2", intent); list.add(getView("ImageActivity2", intent)); intent = new Intent(MainActivity.this,ImageActivity3.class); getView("ImageActivity3", intent); list.add(getView("ImageActivity3", intent)); intent = new Intent(MainActivity.this,ImageActivity4.class); getView("ImageActivity4", intent); list.add(getView("ImageActivity4", intent)); intent = new Intent(MainActivity.this,ImageActivity5.class); getView("ImageActivity5", intent); list.add(getView("ImageActivity5", intent)); imageViews = new ImageView[list.size()]; LinearLayout linearLayout = (LinearLayout) findViewById(R.id.viewGroup); viewPager = (ViewPager)findViewById(R.id.viewPager); for (int i = 0; i < list.size(); i ) { imageViews[i] = new ImageView(this); imageViews[i].setLayoutParams(new LayoutParams(12,12)); if (i == 0) { imageViews[i].setBackgroundResource(R.drawable.white); } else { imageViews[i].setBackgroundResource(R.drawable.black); } linearLayout.addView(imageViews[i]); } viewPager.setAdapter(new MyAdapter()); viewPager.setOnPageChangeListener(new MyListener()); viewPager.setCurrentItem(300); mHandler.postDelayed(mRunnable, 2000); } private Handler mHandler = new Handler(); private Runnable mRunnable = new Runnable() { public void run() { //姣忛殧澶氶暱鏃堕棿鎵ц涓� //mHandler.postDelayed(mRunnable, 1000*PhoneConstans.TIMEVALUE); mHandler.postDelayed(mRunnable, 1000*2); num ; viewHandler.sendEmptyMessage(num); } }; private View getView(String id,Intent intent){ return manager.startActivity(id, intent).getDecorView(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_BACK: this.finish(); mHandler.removeCallbacks(mRunnable); default: break; } return super.onKeyDown(keyCode, event); } private final Handler viewHandler = new Handler() { @Override public void handleMessage(Message msg) { viewPager.setCurrentItem(msg.what); super.handleMessage(msg); } }; class MyAdapter extends PagerAdapter { @Override public int getCount() { return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } @Override public int getItemPosition(Object object) { // TODO Auto-generated method stub return super.getItemPosition(object); } @Override public void destroyItem(View arg0, int arg1, Object arg2) { // TODO Auto-generated method stub ((ViewPager) arg0).removeView(list.get(arg1%list.size())); } @Override public Object instantiateItem(View arg0, int arg1) { // TODO Auto-generated method stub try{ //((ViewPager) arg0).addView(list.get(arg1),0); ((ViewPager) arg0).addView((View) list.get(arg1%list.size()),0); }catch (Exception e) { // TODO: handle exception } return list.get(arg1%list.size()); } @Override public void restoreState(Parcelable arg0, ClassLoader arg1) { // TODO Auto-generated method stub } @Override public Parcelable saveState() { // TODO Auto-generated method stub return null; } @Override public void startUpdate(View arg0) { // TODO Auto-generated method stub } @Override public void finishUpdate(View arg0) { // TODO Auto-generated method stub } } class MyListener implements OnPageChangeListener { //褰撴粦鍔ㄧ姸鎬佹敼鍙樻椂璋冪敤 @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub //arg0=arg0%list.size(); } //褰撳綋鍓嶉〉闈㈣婊戝姩鏃惰皟鐢� @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } //褰撴柊鐨勯〉闈㈣閫変腑鏃惰皟鐢� @Override public void onPageSelected(int arg0) { num = arg0; arg0=arg0%list.size(); for (int i = 0; i < imageViews.length; i ) { imageViews[arg0].setBackgroundResource(R.drawable.white); if (arg0 != i) { imageViews[i].setBackgroundResource(R.drawable.black); } } System.out.println( "褰撳墠鏄" arg0); } } }
评论