<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content"> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Page1" /> </LinearLayout>
package com.rainsong.viewpagerdemo; import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.LayoutInflater; import android.view.View; import android.widget.Toast; public class MainActivity extends Activity implements OnPageChangeListener { ViewPager mViewPager; ViewPagerAdapter mViewPagerAdapter; ArrayList<View> mViews; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 将要分页显示的View装入数组中 LayoutInflater inflater = getLayoutInflater(); mViews = new ArrayList<View>(); mViews.add(inflater.inflate(R.layout.viewpager_page1, null)); mViews.add(inflater.inflate(R.layout.viewpager_page2, null)); mViews.add(inflater.inflate(R.layout.viewpager_page3, null)); mViews.add(inflater.inflate(R.layout.viewpager_page4, null)); mViewPager = (ViewPager) findViewById(R.id.viewpager); mViewPagerAdapter = new ViewPagerAdapter(mViews); // 设置ViewPager的适配器和监听事件 mViewPager.setAdapter(mViewPagerAdapter); mViewPager.setOnPageChangeListener(this); } @Override public void onPageScrollStateChanged(int arg0) { } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageSelected(int position) { int pageId = position + 1; Toast.makeText(MainActivity.this, "Page" + pageId + " Selected", Toast.LENGTH_SHORT).show(); } }
package com.rainsong.viewpagerdemo; import java.util.ArrayList; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.View; import android.view.ViewGroup; public class ViewPagerAdapter extends PagerAdapter { private ArrayList<View> views; public ViewPagerAdapter (ArrayList<View> views){ this.views = views; } @Override public int getCount() { if (views != null) { return views.size(); } return 0; } @Override public Object instantiateItem(ViewGroup view, int position) { ((ViewPager) view).addView(views.get(position), 0); return views.get(position); } @Override public void destroyItem(ViewGroup view, int position, Object arg2) { ((ViewPager) view).removeView(views.get(position)); } @Override public boolean isViewFromObject(View view, Object arg1) { return (view == arg1); } }
原文地址:http://blog.csdn.net/hantangsongming/article/details/41720997