Android中ViewPage的使用

 

ViewPage的使用

package com.gfive.help;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.gfive.help.tools.Tools;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.Toast;

public class ViewPageActivity extends Activity {
	ViewPager viewPager;
	ArrayList<View> list;
	ViewGroup main, group;
	ImageView imageView;
	ImageView[] imageViews;
	private static int c_id = 0;
	private static final String IMAGE_PATH = "/gfive/images";
	/**
	 * 图片类型
	 */
	public static final String IMAGE_TYPE = "image_type";
	
	private String imageType;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		if(!Tools.existSDcard(this)){//如果没有内存卡,请绕开
				finish();
		}
		imageType = getIntent().getStringExtra(IMAGE_TYPE);		//获取图片类型
		LayoutInflater inflater = getLayoutInflater();
		List<Bitmap> lists = getBitmaps();
		list = new ArrayList<View>();
		ImageView iv = null;
		for(int i = 0;i<lists.size();i++){
			iv = new ImageView(this);
			iv.setImageBitmap(lists.get(i));
			list.add(iv);
		}
		imageViews = new ImageView[list.size()];
		ViewGroup main = (ViewGroup) inflater.inflate(R.layout.tutorials_layout, null);
		ViewGroup group = (ViewGroup) main.findViewById(R.id.viewGroup);
		viewPager = (ViewPager) main.findViewById(R.id.viewPager);
		for (int i = 0; i < list.size(); i++) {
			imageView = new ImageView(ViewPageActivity.this);
			imageView.setLayoutParams(new LayoutParams(12, 12));
			imageViews[i] = imageView;
			if (i == 0) {
				imageViews[i].setBackgroundResource(R.drawable.guide_dot_white);
			} else {
				imageViews[i].setBackgroundResource(R.drawable.guide_dot_black);
			}
			group.addView(imageView);
		}
		setContentView(main);
		viewPager.setAdapter(new MyAdapter());
		viewPager.setOnPageChangeListener(new MyListener());
		viewPager.setCurrentItem(300);
	}

	class MyAdapter extends PagerAdapter {
		@Override
		public int getCount() {
			return Integer.MAX_VALUE;
		}

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

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

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			try {
				((ViewPager) arg0).addView(list.get(arg1 % list.size()), 0);
			} catch (Exception e) {
			}
			return list.get(arg1 % list.size());
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
		}

		@Override
		public void finishUpdate(View arg0) {
		}
	}

	class MyListener implements OnPageChangeListener {
		// 当滑动状态改变时调用
		@Override
		public void onPageScrollStateChanged(int arg0) {

		}

		// 当当前页面被滑动时调用
		@Override
		public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
			if(position==list.size()-1){	//最后一页是关掉帮助
				finish();
			}
		}

		// 当新的页面被选中时调用
		@Override
		public void onPageSelected(int location) {
			if (location > 2 && list.size()>0) {
				location = location % list.size();
			}
			c_id = location;
			for (int i = 0; i < imageViews.length; i++) {
				imageViews[location].setBackgroundResource(R.drawable.guide_dot_white);
				if (location != i) {imageViews[i].setBackgroundResource(R.drawable.guide_dot_black);
				}
			}
		}
	}
	
	@Override
	protected void onDestroy() {
		finish();
		super.onDestroy();
		
	}
	
	/**
	 * 获取手机中的图片集合
	 * @return
	 */
	private List<Bitmap> getBitmaps(){
		int i = 1;
		List<Bitmap> bitmaps = new ArrayList<Bitmap>();
		StringBuilder sbPath = new StringBuilder();
		sbPath.append(Tools.SDCARD_STORAGE_PATH).append(IMAGE_PATH).append(imageType);
		while(true){
			File file = new File(sbPath.toString()+("guide_img"+i+".png").toString());
			if(!file.exists()) break;
			bitmaps.add(BitmapFactory.decodeFile(sbPath.toString()+("guide_img"+i+".png")));
			i++;
		}
		return bitmaps;
	}
}

  布局文件:

<?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" /> 
		</RelativeLayout> 
</FrameLayout> 

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值