layout.xml
java.class
public class MainActivity extends Activity{ private static String TAG = "MainActivity"; /** * Viewpager对象 */ private ViewPager mViewPager; /** * Viewpager的适配器 */ private ViewPagerAdapter mVpAdapter; /** * 存放view的列表 */ private Listadapter.classmViews; /** * 底部小点图片 */ private ImageView[] dots; /** * 记录当前view的位置 */ private int currentIndex; /** * 记录当前自动滑动的状态,true就滑动,false停止滑动 */ private boolean isContinue = true; private Handler mHandler; private Timer timer; private static boolean isSleep = true; /** * 设置viewpager的初始页面 */ private static final int initPositon = 50000; /** * viewpager的当前页面 */ private static int currentPosition = initPositon; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mViews = new ArrayList (); LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT); Resources res = getResources(); for (int i = 0; i < Images.mPics.length; i++) { ImageView mImageView = new ImageView(this); mImageView.setLayoutParams(mParams); Bitmap bitmap = BitmapFactory.decodeResource(res, Images.mPics[i]); mImageView.setImageBitmap(bitmap); mViews.add(mImageView); } // Handler对象更新UI mHandler = new Handler() { public void handleMessage(Message msg) { mViewPager.setCurrentItem(currentPosition); } }; //启动线程,监控是否要自动滑动 timer = new Timer(); timer.schedule(new TimerTask() { public void run() { while (true) { if (isContinue) { /* if (isSleep) { sleep(4000); } */ currentPosition ++; mHandler.sendEmptyMessage(0); sleep(3000); /* if (!isSleep) { sleep(3000); } isSleep = false;*/ } } } }, 4000); mViewPager = (ViewPager) findViewById(R.id.viewpager); mVpAdapter = new ViewPagerAdapter(mViews); mViewPager.setAdapter(mVpAdapter); mViewPager.setCurrentItem(initPositon); mViewPager.setOnPageChangeListener(new MyPageChangeListener()); mViewPager.setOnTouchListener(new MyTouchListener()); initDots(); } /** * 底部圆点初始化 */ private void initDots() { LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.llyt_welcome_dot); dots = new ImageView[Images.mPics.length]; for (int i = 0; i < Images.mPics.length; i++) { dots[i] = (ImageView) mLinearLayout.getChildAt(i); // 设置为灰色 dots[i].setEnabled(true); // 为每一个imageview添加点击监听 // dots[i].setOnClickListener(new MyOnClickListener()); // 设置位置tag,方面取出与当前位置对应 dots[i].setTag(i); } currentIndex = 0; //设置第一个圆点高亮 dots[currentIndex].setEnabled(false); } /** * 设置底部圆点的高亮 * @param position * 将要显示的view的位置 */ private void setCurrentDot(int position) { if (position < 0 || position > Images.mPics.length - 1) { return; } dots[position].setEnabled(false); dots[currentIndex].setEnabled(true); currentIndex = position; } /* * class MyOnClickListener implements OnClickListener{ * * @Override public void onClick(View v) { int position = (Integer) * v.getTag(); mViewPager.setCurrentItem(position); } * * } */ /** * viewpager页面变化的监听器 * @author user * */ class MyPageChangeListener implements OnPageChangeListener { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { // TODO Auto-generated method stub } @Override public void onPageSelected(int position) { setCurrentDot(position % mViews.size()); currentPosition = position; } @Override public void onPageScrollStateChanged(int state) { // TODO Auto-generated method stub } } /** * 监听手势监听器 * @author user * */ class MyTouchListener implements OnTouchListener { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: isContinue = false; break; case MotionEvent.ACTION_UP: default: isContinue = true; isSleep = true; break; } return false; } } /** * 设置线程间隔 */ private void sleep(long time){ try { Thread.sleep(time); } catch (Exception e) { e.printStackTrace(); } }}
public class ViewPagerAdapter extends PagerAdapter{ private static String TAG = "ViewPagerAdapter"; /** * 界面列表 */ private ListmViews; public ViewPagerAdapter(List mViews) { this.mViews = mViews; } /** * 获取当前页面数 */ @Override public int getCount() { // Log.v(TAG, "getCount" + mViews.size()); return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View view, Object object) { // Log.v(TAG, "isViewFromObject" + (view == object)); return view == object; } /** * 适配器给container容器添加视图 */ @Override public Object instantiateItem(ViewGroup container, int position) { Log.v(TAG, "instantiateItem" + position); position = position % mViews.size(); container.addView(mViews.get(position), 0); return mViews.get(position); } /** * 适配器移除container容器中的视图 */ @Override public void destroyItem(ViewGroup container, int position, Object object) { Log.v(TAG, "destroyItem" + position); position = position % mViews.size(); container.removeView(mViews.get(position)); } }