亲宝软件园·资讯

展开

Android使用ViewPager实现左右无限滑动 Android使用ViewPager实现左右无限滑动

mufeng_慕枫 人气:0

前言

        网上有很多使用ViewPager实现左右滑动这一效果的资料,这些资料大多数都是将PagerAdapter中getCount()方法的返回值设为Integer.MAX_VALUE使用户看不到边界,然后在instantiateItem()方法中通过position%(要循环显示的数据集的长度)的方式取得对应的数据集。这样虽然可以做到无限循环,但是会有两个弊端:首先会创建大量对象,容易引起内存溢出(循环加载图片)从而影响性能;其次从第一页向右滑动的时候是无法滑动的。

       今天给大家分享的是另一种实现方式:创建三个图片视图放入ViewPager中默认选中第二页,在接下来的滑动中每次滑动结束之后都将当前页码设置为第二页,然后通过判断是向左向右滑动来设置视图中的数据集,这样就可以实现无限循环了,而且只会创建三个图片视图对象。

示例源码

package com.viewpager; 
 
import java.util.ArrayList; 
import java.util.List; 
import android.os.Bundle; 
import android.support.v4.view.PagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.support.v4.view.ViewPager.OnPageChangeListener; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.app.Activity; 
 
public class MainActivity01 extends Activity implements OnPageChangeListener { 
 
 private ViewPager viewPager; 
 static final int arrays[] = { R.drawable.guide1, R.drawable.guide2, 
   R.drawable.guide3, R.drawable.guide4, R.drawable.guide5 }; 
 private List<ImageView> views; 
 private int currentPage = 0; 
 private ImageView imageView; 
 
 private MyViewPagerAdapter viewPagerAdapter; 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.activity_main); 
  initWidget(); 
 } 
 
 private void initWidget() { 
  viewPager = (ViewPager) findViewById(R.id.viewpager); 
 
  views = new ArrayList<ImageView>(); 
  for (int i = 0; i < 3; i++) { 
   imageView = new ImageView(this); 
   imageView.setLayoutParams(new ViewGroup.LayoutParams( 
     ViewGroup.LayoutParams.MATCH_PARENT, 
     ViewGroup.LayoutParams.MATCH_PARENT)); 
   views.add(imageView); 
  } 
 
  initImageData(); 
 
  viewPagerAdapter = new MyViewPagerAdapter(); 
  viewPager.setAdapter(viewPagerAdapter); 
  viewPager.setCurrentItem(1); 
  viewPager.setOnPageChangeListener(this); 
 } 
 
 private void initImageData() { 
  for (int i = 0; i < 3; i++) { 
   imageView = views.get(i); 
   if (i == 0) { 
    imageView.setImageResource(arrays[arrays.length - 1]); 
   } else { 
    imageView.setImageResource(arrays[i - 1]); 
   } 
  } 
 } 
 
 class MyViewPagerAdapter extends PagerAdapter { 
 
  @Override 
  public int getCount() { 
   // TODO Auto-generated method stub 
   return views.size(); 
  } 
 
  @Override 
  public boolean isViewFromObject(View arg0, Object arg1) { 
   // TODO Auto-generated method stub 
   return arg0 == arg1; 
  } 
 
  @Override 
  public Object instantiateItem(ViewGroup container, int position) { 
   imageView = views.get(position); 
   container.addView(imageView); 
   return imageView; 
  } 
 
  @Override 
  public void destroyItem(ViewGroup container, int position, Object object) { 
   // TODO Auto-generated method stub 
   container.removeView((View) object); 
  } 
 
 } 
 
 // 当滑动状态改变时调用 
 @Override 
 public void onPageScrollStateChanged(int state) { 
  // TODO Auto-generated method stub 
  System.out.println("--onPageScrollStateChanged--state--:" + state); 
  switch (state) { 
  // 在滚动完成后 
  case ViewPager.SCROLL_STATE_IDLE: 
   int currentItem = viewPager.getCurrentItem(); 
 
   System.out.println("--currentItem--00--:" + currentItem); 
   System.out.println("--currentPage--00--:" + currentPage); 
   if (viewPager.getCurrentItem() == 1) { 
    // 如果位置没有变终止循环 
    break; 
   } 
 
   if (viewPager.getCurrentItem() > 1) { 
    currentPage++; 
   } else { 
    currentPage--; 
   } 
 
   System.out.println("--currentPage--11--:" + currentPage); 
   if (currentPage == arrays.length) { 
    currentPage = 0; 
   } 
 
   if (currentPage == -1) { 
    currentPage = arrays.length - 1; 
   } 
 
   System.out.println("--currentPage--22--:" + currentPage); 
 
   if (currentPage == 0) { 
    views.get(0).setImageResource(arrays[arrays.length - 1]); 
   } else { 
    views.get(0).setImageResource(arrays[currentPage - 1]); 
   } 
 
   views.get(1).setImageResource(arrays[currentPage]); 
 
   if (currentPage == arrays.length - 1) { 
    views.get(2).setImageResource(arrays[0]); 
   } else { 
    views.get(2).setImageResource(arrays[currentPage + 1]); 
   } 
 
   viewPager.setCurrentItem(1, false); 
 
   currentItem = viewPager.getCurrentItem(); 
 
   System.out.println("--currentItem--11--:" + currentItem); 
   break; 
  } 
 } 
 
 // 当当前页面被滑动时调用 
 @Override 
 public void onPageScrolled(int position, float positionOffset, 
   int positionOffsetPixels) { 
  // TODO Auto-generated method stub 
  // System.out.println("--onPageScrolled--position--:" + position); 
 } 
 
 // 当新的页面被选中时调用 
 @Override 
 public void onPageSelected(int position) { 
  // TODO Auto-generated method stub 
  System.out.println("--onPageSelected--position--:" + position); 
 } 
} 

加载全部内容

相关教程
猜你喜欢
用户评论