PS:
优化后的代码更新在 http://blog.csdn.net/lfdfhl/article/details/51017116
该代码已经不太具有参考价值
MainActivity如下:
package cc.ww; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v4.view.ViewPager; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; import android.view.View.OnTouchListener; import android.widget.ImageView; import android.widget.LinearLayout; /** * @author http://blog.csdn.net/lfdfhl * * Demo描述: * 1 ViewPager的自动轮播 * 2 在实现ViewPager的自动轮播时时常遇到一个问题: * 当item数量小于4(比如2或者3)时ViewPager容易出问题或者Crash. * 在网上看了许多资料都没有很好的解决该问题. * 所以在此给出解决方案,希望能帮到遇到同样问题的人. * 3 同时支持手动切换ViewPager的Item */ public class MainActivity extends Activity { private Context mContext; private Handler mHandler; private Runnable mRunnable; private ViewPager mViewPager; private final int SET_ITEM=9527; private final int INTERVAL=1000*2; private ImageView[] dotImageViews; private LinearLayout mDotsLinearLayout; private LauncherViewPagerAdapter mViewPagerAdapter; private PageChangeListenerImpl mPageChangeListenerImpl; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 去掉状态栏 requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_main); init(); } @SuppressLint("ClickableViewAccessibility") @SuppressWarnings("deprecation") private void init() { mContext = this; mViewPager = (ViewPager) findViewById(R.id.guide_viewpager); mDotsLinearLayout = (LinearLayout) findViewById(R.id.dotsLinearLayout); mViewPagerAdapter = new LauncherViewPagerAdapter(mContext); mPageChangeListenerImpl = new PageChangeListenerImpl(); mViewPager.setAdapter(mViewPagerAdapter); mViewPager.setOnPageChangeListener(mPageChangeListenerImpl); mViewPager.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View arg0, MotionEvent arg1) { mViewPager.requestDisallowInterceptTouchEvent(true); return false; } }); initDots(); setAutoChangeViewPager(); } // 初始化小圆点 private void initDots() { dotImageViews = new ImageView[mViewPagerAdapter.getCount() / mViewPagerAdapter.getMultiple()]; for (int i = 0; i < dotImageViews.length; i++) { LinearLayout layout = new LinearLayout(mContext); ImageView imageView = new ImageView(mContext); imageView.setLayoutParams(new ViewGroup.LayoutParams(20, 20)); if (i == 0) { imageView.setBackgroundResource(R.drawable.guide_dot_white); } else { layout.setPadding(20, 0, 0, 0); imageView.setBackgroundResource(R.drawable.guide_dot_black); } dotImageViews[i] = imageView; layout.addView(imageView); mDotsLinearLayout.addView(layout); } } // ViewPager自动切换 @SuppressWarnings("deprecation") private void setAutoChangeViewPager() { mViewPager.setOnPageChangeListener(mPageChangeListenerImpl); mHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case SET_ITEM: if (null != mViewPager && mViewPagerAdapter != null && mViewPagerAdapter.getCount() > 0) { int curItemIndex = mViewPager.getCurrentItem(); int itemSize = mViewPager.getAdapter().getCount(); if (mViewPagerAdapter.getMultiple() > 1) { curItemIndex = curItemIndex % mViewPagerAdapter.getMultiple(); itemSize = itemSize / mViewPagerAdapter.getMultiple(); if ((curItemIndex + 1) < itemSize) { mViewPager.setCurrentItem(curItemIndex + 1, true); } else { mViewPager.setCurrentItem(0, false); } } else { if ((curItemIndex + 1) < itemSize) { mViewPager.setCurrentItem(curItemIndex + 1, true); } else { mViewPager.setCurrentItem(0, false); } } } break; } } }; mRunnable = new Runnable() { @Override public void run() { Message message = mHandler.obtainMessage(); message.what = SET_ITEM; mHandler.sendMessage(message); mHandler.removeCallbacks(mRunnable); mHandler.postDelayed(this, INTERVAL); } }; mHandler.postDelayed(mRunnable, INTERVAL); } @Override protected void onDestroy() { super.onDestroy(); if (null != mViewPager) { mViewPager.removeAllViews(); mViewPager = null; } } private class PageChangeListenerImpl implements ViewPager.OnPageChangeListener { @Override public void onPageScrollStateChanged(int arg0) { } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageSelected(int selected) { // 显示原点 int count = mDotsLinearLayout.getChildCount(); if (count > 0) { for (int i = 0; i < count; i++) { LinearLayout layout = (LinearLayout) mDotsLinearLayout.getChildAt(i); ImageView imageView = (ImageView) layout.getChildAt(0); if (mViewPagerAdapter.getMultiple() > 1) { if (selected % mViewPagerAdapter.getMultiple() == i) { imageView.setBackgroundResource(R.drawable.guide_dot_white); } else { imageView.setBackgroundResource(R.drawable.guide_dot_black); } } else { if (selected % mViewPagerAdapter.getCount() == i) { imageView.setBackgroundResource(R.drawable.guide_dot_white); } else { imageView.setBackgroundResource(R.drawable.guide_dot_black); } } } } } } }
LauncherViewPagerAdapter如下:
package cc.ww; import android.content.Context; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.widget.ImageView; public class LauncherViewPagerAdapter extends PagerAdapter { private int multiple=1; private Context mContext; //private int[] pageArray = {R.drawable.a,R.drawable.b}; private int[] pageArray = {R.drawable.a,R.drawable.b, R.drawable.c}; //private int[] pageArray = {R.drawable.a,R.drawable.b, R.drawable.c, R.drawable.d}; public LauncherViewPagerAdapter(Context context) { this.mContext = context; } public void setMultiple(int m){ multiple=m; } public int getMultiple(){ return multiple; } @Override public int getCount() { if(pageArray.length<3){ setMultiple(2); return pageArray.length*getMultiple(); } if(pageArray.length<4){ setMultiple(3); return pageArray.length*getMultiple(); } return pageArray.length; } @Override public Object instantiateItem(View container, int position) { View itemView = LayoutInflater.from(mContext).inflate(R.layout.guide_pager_adapter, null); itemView.setFocusable(true); ImageView layout = (ImageView) itemView.findViewById(R.id.imageView); if (getMultiple()>1) { layout.setBackgroundResource(pageArray[position%getMultiple()]); } else { layout.setBackgroundResource(pageArray[position%getCount()]); } ((ViewPager) container).addView(itemView); return itemView; } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } @Override public void destroyItem(View container, int position, Object object) { ((ViewPager) container).removeView((View) object); } }
activity_main.xml如下:
<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:id="@+id/guide_viewpager" android:layout_width="match_parent" android:layout_height="match_parent" /> <LinearLayout android:id="@+id/dotsLinearLayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="100px" android:layout_centerHorizontal="true" android:orientation="horizontal"> </LinearLayout> </RelativeLayout>
guide_pager_adapter.xml如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>