(转载)PagerView实现翻页效果

现在很多应用都在第一次启动时提供一个简要的介绍,以左右滑动的效果来介绍应用的使用和基本情况。下面将实现一个简单的Demo

首先到SDK的extra目录下找到android-support-v4.jar

首先看一下工程结构:

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <android.support.v4.view.ViewPager  
  8.         android:id="@+id/guidePages"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content" />  
  11.   
  12.     <RelativeLayout  
  13.         android:layout_width="fill_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:orientation="vertical" >  
  16.   
  17.         <LinearLayout  
  18.             android:id="@+id/viewGroup"  
  19.             android:layout_width="fill_parent"  
  20.             android:layout_height="wrap_content"  
  21.             android:layout_alignParentBottom="true"  
  22.             android:layout_marginBottom="15dp"  
  23.             android:gravity="center_horizontal"  
  24.             android:orientation="horizontal" >  
  25.         </LinearLayout>  
  26.     </RelativeLayout>  
  27.   
  28. </FrameLayout>  


然后新建page1.xml文件

 

 

  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     android:layout_width="fill_parent"    
  4.     android:layout_height="fill_parent"    
  5.     android:orientation="vertical" >    
  6.         
  7.     <ImageView      
  8.         android:layout_width="fill_parent"      
  9.         android:layout_height="fill_parent"      
  10.         android:background="@drawable/a" >      
  11.     </ImageView>    
  12.         
  13. </LinearLayout>    


page2.xml、page3.xml一样,只要改变图片路径就行

 

然后编辑Activity

 

  1. public class ViewPagerActivity extends Activity {  
  2.       
  3.     private ViewPager viewPager;      
  4.     private ArrayList<View> pageViews;      
  5.     private ViewGroup main;  
  6.     private ViewGroup group;  
  7.     private ImageView imageView;      
  8.     private ImageView[] imageViews;     
  9.       
  10.     @Override  
  11.     public void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         this.requestWindowFeature(Window.FEATURE_NO_TITLE);  
  14.           
  15.         setContentView(R.layout.main);  
  16.           
  17.         LayoutInflater inflater = getLayoutInflater();  
  18.         pageViews = new ArrayList<View>();  
  19.         pageViews.add(inflater.inflate(R.layout.page1, null));  
  20.         pageViews.add(inflater.inflate(R.layout.page2, null));  
  21.         pageViews.add(inflater.inflate(R.layout.page3, null));  
  22.           
  23.         imageViews = new ImageView[pageViews.size()];  
  24.           
  25.         main = (ViewGroup)inflater.inflate(R.layout.main, null);  
  26.           
  27.         //group是底部导航小圆点的布局  
  28.         group = (ViewGroup)main.findViewById(R.id.viewGroup);  
  29.           
  30.         viewPager = (ViewPager)main.findViewById(R.id.guidePages);      
  31.           
  32.         for (int i = 0; i < pageViews.size(); i++) {      
  33.             imageView = new ImageView(ViewPagerActivity.this);      
  34.             imageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));      
  35.             imageViews[i] = imageView;      
  36.             if (i == 0) {      
  37.                 //默认选中第一张图片    
  38.                 imageViews[i].setBackgroundResource(R.drawable.indicator_f);      
  39.             } else {      
  40.                 imageViews[i].setBackgroundResource(R.drawable.indicator);      
  41.             }      
  42.             group.addView(imageViews[i]);      
  43.         }      
  44.       
  45.         setContentView(main);      
  46.       
  47.         viewPager.setAdapter(new ViewPagerAdapter());      
  48.         viewPager.setOnPageChangeListener(new ViewPagerChangeListener());  
  49.           
  50.     }  
  51.       
  52.     class ViewPagerAdapter extends PagerAdapter{  
  53.   
  54.         @Override  
  55.         public int getCount() {  
  56.             return pageViews.size();  
  57.         }  
  58.   
  59.         @Override      
  60.         public boolean isViewFromObject(View arg0, Object arg1) {      
  61.             return arg0 == arg1;      
  62.         }      
  63.       
  64.         @Override      
  65.         public void destroyItem(View arg0, int arg1, Object arg2) {      
  66.             ((ViewPager) arg0).removeView(pageViews.get(arg1));      
  67.         }      
  68.       
  69.         @Override      
  70.         public Object instantiateItem(View arg0, int arg1) {      
  71.             ((ViewPager) arg0).addView(pageViews.get(arg1));      
  72.             return pageViews.get(arg1);      
  73.         }      
  74.       
  75.     }  
  76.       
  77.     class ViewPagerChangeListener implements OnPageChangeListener  
  78.     {  
  79.   
  80.         @Override  
  81.         public void onPageScrollStateChanged(int arg0) {  
  82.             // TODO Auto-generated method stub  
  83.               
  84.         }  
  85.   
  86.         @Override  
  87.         public void onPageScrolled(int arg0, float arg1, int arg2) {  
  88.             // TODO Auto-generated method stub  
  89.               
  90.         }  
  91.   
  92.         @Override  
  93.         public void onPageSelected(int position) {  
  94.             for (int i = 0; i < imageViews.length; i++) {      
  95.                 imageViews[position].setBackgroundResource(R.drawable.indicator_f);      
  96.                 if (position != i) {      
  97.                     imageViews[i].setBackgroundResource(R.drawable.indicator);      
  98.                 }      
  99.             }    
  100.         }  
  101.     }  
  102. }  


运行,效果图如下,不是很美观,实际使用替换图片就行,需要源码的童鞋可以联系我。我的新浪微博 唐韧_Ryan

 

posted @ 2012-12-25 11:12  GreyWolf  阅读(616)  评论(0编辑  收藏  举报