效果图:
main.xml:
<?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/main_viewpager" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" > </android.support.v4.view.ViewPager> </LinearLayout>common.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" android:gravity="center" android:orientation="vertical" > <TextView android:gravity="center_horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="OnlyC" android:textSize="25sp" android:textColor="@android:color/black" /> </LinearLayout>two.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/w01" android:gravity="center" android:orientation="vertical" > <TextView android:gravity="center_horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="TwoC" android:textSize="25sp" android:textColor="@android:color/white" /> </LinearLayout>three.xml four.xml类似,不再列出
MainActivity.java:
package misc.demo; import java.util.ArrayList; import android.app.ActivityGroup; import android.content.Intent; import android.os.Bundle; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.View; import android.view.Window; public class MainActivity extends ActivityGroup { private View oneView; private View twoView; private View threeView, fourView; private ArrayList<View> views; private ViewPager mViewPager;// 多页面滑动切换效果 /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); views = new ArrayList<View>(); mViewPager = (ViewPager) findViewById(R.id.main_viewpager); mViewPager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageScrollStateChanged(int arg0) { // TODO 自动生成的方法存根 System.out.println("onPageScrollStateChanged"); } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO 自动生成的方法存根 System.out.println("onPageScrolled"); } @Override public void onPageSelected(int arg0) { // TODO 自动生成的方法存根 System.out.println("onPageSelected"); } }); initView(); addViews(); PagerAdapter mPagerAdapter = new PagerAdapter() { // 判断是否由对象生成界面 @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } // 获取当前窗体界面数 @Override public int getCount() { System.out.println("getCount" + views.size()); return views.size(); } // 销毁position位置的界面 @Override public void destroyItem(View container, int position, Object object) { ((ViewPager) container).removeView(views.get(position)); System.out.println("destroyItem" + position); } // 初始化position位置的界面 @Override public Object instantiateItem(View container, int position) { ((ViewPager) container).addView(views.get(position)); System.out.println("instantiateItem" + position); return views.get(position); } }; mViewPager.setAdapter(mPagerAdapter); } /** * 将相应的Activity转换成View对象 */ public void initView() { oneView = getViews(OneActivity.class, "one"); twoView = getViews(TwoActivity.class, "one"); threeView = getViews(ThreeActivity.class, "one"); fourView = getViews(FourActivity.class, "one"); } /** * 将转换成View后的Activity添加到列表中 */ public void addViews() { views.add(oneView); views.add(twoView); views.add(threeView); views.add(fourView); } /** * 获取要跳转的Activity对应的View * * @param cls目的Activity * @param pageid目的Activity的唯一id * @return 返回一个View类型的变量 */ public View getViews(Class<?> cls, String pageid) { return getLocalActivityManager().startActivity( pageid, new Intent(MainActivity.this, cls) .addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY)) .getDecorView(); } }OneActivity.java:
package misc.demo; import android.app.Activity; import android.os.Bundle; public class OneActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.common); } }完整工程代码:http://download.csdn.net/detail/u014071669/7187085