Android ViewPager实现软件的第一次加载的滑动效果

public class MainActivity extends Activity {

    private ViewPager viewPager;
    private List<View> Views;
    private ImageView[] imgs;
    private ViewGroup main,group;
    private ImageView img;
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        
        Views=new ArrayList<View>();
        main=(ViewGroup) View.inflate(this,R.layout.main, null);
        
        viewPager=(ViewPager) main.findViewById(R.id.viewpager);
        group=(ViewGroup) main.findViewById(R.id.viewgroup);
        
        View view1=View.inflate(this, R.layout.item0, null);
        View view2=View.inflate(this, R.layout.item1, null);
        View view3=View.inflate(this, R.layout.item2, null);
        View view4=View.inflate(this, R.layout.item4, null);
        
        Views.add(view4);
        Views.add(view3);
        Views.add(view2);
        Views.add(view1);
        
        imgs=new ImageView[Views.size()];
        
        for(int i=0;i<Views.size();i++)
        {
            img=new ImageView(this);
            img.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
            img.setPadding(10, 10, 10, 10);
            if(i==0)
            {
                img.setImageResource(R.drawable.white);
            }else
            {
                img.setImageResource(R.drawable.black);
            }
            group.addView(img);
            imgs[i]=img;
        }
        
        setContentView(main);
        //添加数据
        viewPager.setAdapter(new MyAdapter());
        //监听滑动效果
        viewPager.setOnPageChangeListener(new OnPageChangeListener() 
        {
            public void onPageSelected(int arg0) 
            {
                for(int i=0;i<Views.size();i++)
                {
                    if(arg0!=i)
                    {
                        imgs[i].setImageResource(R.drawable.black);
                    }else
                    {
                        imgs[i].setImageResource(R.drawable.white);
                    }
                }
                
            }
            
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2){ }
            public void onPageScrollStateChanged(int arg0){}
        });
    }

    class MyAdapter extends PagerAdapter
    {

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

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) 
        {
            return arg0==arg1;
        }

        
        @Override
        public int getItemPosition(Object object) 
        {
            return super.getItemPosition(object);
        }

        
        public void destroyItem(View container, int position, Object object) {
            ((ViewPager) container).removeView(Views.get(position));
            
        }

        @Override
        public Object instantiateItem(View container, int position) {
            ((ViewPager) container).addView(Views.get(position));
            return Views.get(position);
        }

        
        
    }
    

}

xml布局文件

<?xml version="1.0" encoding="utf-8"?>  
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:orientation="vertical" >  
  
    <android.support.v4.view.ViewPager  
        android:id="@+id/viewpager"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content" />  
  
    <RelativeLayout  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:orientation="vertical" >  
  
        <LinearLayout  
            android:id="@+id/viewgroup"  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:layout_alignParentBottom="true"  
            android:layout_marginBottom="30dp"  
            android:gravity="center_horizontal"  
            android:orientation="horizontal" >  
        </LinearLayout>  
    </RelativeLayout>  
  
</FrameLayout>  

最后一点是,记得引入android-support-v4.jar

运行效果:

posted on 2013-08-13 11:12  I love Android  阅读(444)  评论(0编辑  收藏  举报