是个传颂厨

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

简单的

先写个布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <android.support.v4.view.ViewPager
        android:id="@+id/id_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ></android.support.v4.view.ViewPager>

</RelativeLayout>

然后java文件

package com.example.deemo;

import java.util.ArrayList;
import java.util.List;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class MainActivity extends ActionBarActivity {

    private ViewPager mViewPager;//声明
    
    private int[] mImgIds = new int[]
            {R.drawable.image_1,R.drawable.image_2,R.drawable.image_3,
            R.drawable.image_4,R.drawable.image_5,R.drawable.image_6};//图片源
   
    private List<ImageView> mImages = new ArrayList<ImageView>();//数组适配器
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
        
        //添加切换效果,3.0以上啊
        //mViewPager.setPageTransformer(true, arg1);//谷歌上有~官方的
        
        mViewPager.setAdapter(new PagerAdapter() {//内部类实现
            
            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                ImageView imageView = new ImageView(MainActivity.this);
                imageView.setImageResource(mImgIds[position]);
                imageView.setScaleType(ScaleType.CENTER_CROP);
                container.addView(imageView);
                mImages.add(imageView);
                return imageView;
            }
            
            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {

                container.removeView(mImages.get(position));
            }
            
            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }
            
            @Override
            public int getCount() {//页数
                return mImgIds.length;
            }
        });
    }

}

单纯的简单切换页面就完成了

 

posted on 2016-08-11 15:25  是个传颂厨  阅读(120)  评论(0编辑  收藏  举报