KnightBubble / android-notes

android-notes
0 stars 1 forks source link

ViewPager-1 #7

Open KnightBubble opened 9 years ago

KnightBubble commented 9 years ago
package com.example.androidstudytest;

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

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

public class ViewPagerTest extends Activity {
    int ids[];
    List<ImageView> imgs;
    ViewPager vp ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_page_test);
        imgs = new ArrayList<ImageView>();
        ids = new int[]{R.drawable.guide_image1,R.drawable.guide_image2,R.drawable.guide_image3};
        vp = (ViewPager) findViewById(R.id.view_pager_wraper);
        vp.setAdapter(new PagerAdapter(){

            /**
             * 销毁一项
             */
            @Override
            public void destroyItem(ViewGroup container, int position,
                    Object object) {
                // TODO Auto-generated method stub
                container.removeView(imgs.get(position));
            }
            /**
             * 初始化
             */
            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                ImageView imageView = new ImageView(ViewPagerTest.this);
                imageView.setImageResource(ids[position]);
                imageView.setScaleType(ScaleType.CENTER_CROP);
                imgs.add(imageView);
                container.addView(imageView);
                return imageView;
            }
            /**
             *  多少页
             */
            @Override
            public int getCount() {
                return ids.length;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                // TODO Auto-generated method stub
                return view == object;
            }

        });
    }

}
KnightBubble commented 9 years ago
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
     >
     <android.support.v4.view.ViewPager
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:id="@+id/view_pager_wraper"
         >

     </android.support.v4.view.ViewPager>

</RelativeLayout>