[控件]ViewPager

package com.example.pic;

import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.PagerTitleStrip;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;

public class secActivity extends Activity {

	private ViewPager mViewPager;
	private PagerTitleStrip mPagerTitleStrip;
	ArrayList<View> views;
	ArrayList<String> titles;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sec);
		mViewPager = (ViewPager) findViewById(R.id.viewpager);
		mPagerTitleStrip = (PagerTitleStrip) findViewById(R.id.pagertitle);
		// 将要分页显示的View装入数组中
		LayoutInflater mLi = LayoutInflater.from(this);
		// 放置布局文件
		View view1 = mLi.inflate(R.layout.q, null);
		View view2 = mLi.inflate(R.layout.w, null);
		View view3 = mLi.inflate(R.layout.e, null);

		// 每个页面的Title数据
		views = new ArrayList<View>();
		views.add(view1);
		views.add(view2);
		views.add(view3);
		titles = new ArrayList<String>();
		titles.add("title1");
		titles.add("title2");
		titles.add("title3");
		// 填充ViewPager的数据适配器
		PagerAdapter mPagerAdapter = new PagerAdapter() {
                        // 判断是否由对象生成界面
			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {
				return arg0 == arg1;
			}
                        //获取当前窗体界面数
			@Override
			public int getCount() {
				return views.size();
			}
                        //销毁position位置的界面
			@Override
			public void destroyItem(View container, int position, Object object) {
				((ViewPager) container).removeView(views.get(position));
			}

			// 显示标题
			@Override
			public CharSequence getPageTitle(int position) {
				return titles.get(position);
			}
                        //初始化position位置的界面
			@Override
			public Object instantiateItem(View container, int position) {
				((ViewPager) container).addView(views.get(position));
				//获取当前页面控件
				Button b = (Button) container.findViewById(R.id.btn_1);
				return views.get(position);
			}
		};
		mViewPager.setAdapter(mPagerAdapter);
		// 监听页面改变事件
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
                        //状态有三个0空闲,1是增在滑行中,2目标加载完毕
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
                        //第一个参数向右拖动时为前一页的页码向左拖动时是当前页的页码(页码从0开始)
                        //第二个为拖动了多少百分比0-1
                        //第三个为拖动了多少像素与分辨率有关
			}

			// 改变完成事件
			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
		// mViewPager.setCurrentItem(item);设置当前页数
		// mViewPager.getCurrentItem();获取当前页数
	}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" >

        <android.support.v4.view.PagerTitleStrip
            android:id="@+id/pagertitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top" />
    </android.support.v4.view.ViewPager>

</LinearLayout>
设置viewpager中的控件点击事件
public void start(View v) {//在button等控件的xml文件中添加android:onClick="start"
		switch (v.getId()) {
		case R.id.btn_1:
		case R.id.btn_2:
			mViewPager.setCurrentItem(++index);
			break;
		case R.id.btn_3:
			finish();
			break;
		}
	}

[控件]ViewPager_第1张图片|


如果想把布局文件换成Activity
首先初始化
LocalActivityManager manager = new LocalActivityManager(this , true);
manager.dispatchCreate(savedInstanceState);
manager.startActivity(id, intent).getDecorView();
在添加布局文件是改为
listViews.add(getLocalActivityManager().startActivity("1", new Intent(Hdjl.this, Hdjl_cx.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView());



版权声明:本文为博主原创文章,未经博主允许不得转载。

你可能感兴趣的:([控件]ViewPager)