PagerView实现翻页效果

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

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

首先看一下工程结构:





    

    

        
        
    


然后新建page1.xml文件

  
  
      
        
      
      
  

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

然后编辑Activity

public class ViewPagerActivity extends Activity {
	
	private ViewPager viewPager;    
    private ArrayList pageViews;    
    private ViewGroup main;
    private ViewGroup group;
    private ImageView imageView;    
    private ImageView[] imageViews;   
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        setContentView(R.layout.main);
        
        LayoutInflater inflater = getLayoutInflater();
        pageViews = new ArrayList();
        pageViews.add(inflater.inflate(R.layout.page1, null));
        pageViews.add(inflater.inflate(R.layout.page2, null));
        pageViews.add(inflater.inflate(R.layout.page3, null));
        
        imageViews = new ImageView[pageViews.size()];
        
        main = (ViewGroup)inflater.inflate(R.layout.main, null);
        
        //group是底部导航小圆点的布局
        group = (ViewGroup)main.findViewById(R.id.viewGroup);
        
        viewPager = (ViewPager)main.findViewById(R.id.guidePages);    
        
        for (int i = 0; i < pageViews.size(); i++) {    
            imageView = new ImageView(ViewPagerActivity.this);    
            imageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));    
            imageViews[i] = imageView;    
            if (i == 0) {    
                //默认选中第一张图片  
                imageViews[i].setBackgroundResource(R.drawable.indicator_f);    
            } else {    
                imageViews[i].setBackgroundResource(R.drawable.indicator);    
            }    
            group.addView(imageViews[i]);    
        }    
    
        setContentView(main);    
    
        viewPager.setAdapter(new ViewPagerAdapter());    
        viewPager.setOnPageChangeListener(new ViewPagerChangeListener());
        
    }
    
    class ViewPagerAdapter extends PagerAdapter{

		@Override
		public int getCount() {
			return pageViews.size();
		}

		@Override    
        public boolean isViewFromObject(View arg0, Object arg1) {    
            return arg0 == arg1;    
        }    
    
        @Override    
        public void destroyItem(View arg0, int arg1, Object arg2) {    
            ((ViewPager) arg0).removeView(pageViews.get(arg1));    
        }    
    
        @Override    
        public Object instantiateItem(View arg0, int arg1) {    
            ((ViewPager) arg0).addView(pageViews.get(arg1));    
            return pageViews.get(arg1);    
        }    
    
    }
    
    class ViewPagerChangeListener implements OnPageChangeListener
    {

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onPageSelected(int position) {
			for (int i = 0; i < imageViews.length; i++) {    
                imageViews[position].setBackgroundResource(R.drawable.indicator_f);    
                if (position != i) {    
                    imageViews[i].setBackgroundResource(R.drawable.indicator);    
                }    
            }  
		}
    }
}

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





对Android&IOS感兴趣的朋友可以加入我们的讨论QQ群,在这里,我们只讨论干货:

iOS群:220223507

Android群:282552849


欢迎关注我的新浪微博和我交流:@唐韧_Ryan


你可能感兴趣的:(Android)