标签:
实现逻辑参考:http://www.cnblogs.com/xinye/archive/2013/06/09/3129140.html
代码:如下
public class MainActivity extends Activity implements OnPageChangeListener { private ViewPager viewpager; private int view[] = { R.layout.a, R.layout.b, R.layout.c }; private List<View> data = new ArrayList<View>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); initViewPagerData(); } private void initViewPagerData() { data.add(0, LayoutInflater.from(this).inflate(view[view.length - 1], null)); for (int i = 0; i < view.length; i++) { data.add(i + 1, LayoutInflater.from(this).inflate(view[i], null)); } data.add(view.length + 1, LayoutInflater.from(this).inflate(view[0], null)); viewpager.setAdapter(new ViewPagerAdapter(data, this)); viewpager.setCurrentItem(1, false); } private void init() { // 初始化界面 viewpager = (ViewPager) findViewById(R.id.viewpager); // 添加事件 viewpager.setOnPageChangeListener(this); } @Override public void onPageScrollStateChanged(int arg0) { } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // 无限循环滑动 if (view.length > 1) {// 多于一个view才会循环跳转 if (arg0 == 0 && arg2 == 0) { // 切换循环更加流畅,不会出现生硬的切换感觉 arg0 = view.length; viewpager.setCurrentItem(arg0, false); } else if (arg0 > view.length) { viewpager.setCurrentItem(1, false); } } } @Override public void onPageSelected(int arg0) { } }
标签:
原文地址:http://www.cnblogs.com/jiayaguang/p/4548312.html