<喎?http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+yOfNvKOsYW5kcm9pZNbQyrnTw1ZpZXdQYWdlcr/J0tTKtc/WtuC49tKzw+a7rLav0Ke5+6GiPC9wPgo8cD48YnI+CjwvcD4KPHA+PC9wPgo8cHJlIGNsYXNzPQ=="brush:java;">public class TvControlActivity extends Activity {private TvControlActivity tvControlActivity = this;// 需要滑動的頁卡private View view1;private View view2;private View view3;private ArrayList views;// 多頁面滑動切換private ViewPager viewPager;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_tv_control);viewPager = (ViewPager) findViewById(R.id.main_viewpager);viewPager.setOnPageChangeListener(null);initView();/** * 這是重寫PagerAdapter的一個方法,我們還可以這樣做 */PagerAdapter paAdapter = new PagerAdapter() {@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {// TODO Auto-generated method stubreturn arg0 == arg1;}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn views.size();}//銷毀@Overridepublic void destroyItem(ViewGroup container, int position,Object object) {// TODO Auto-generated method stubcontainer.removeView(views.get(position));}//@Override//public void destroyItem(View container, int position, Object object) {//// TODO Auto-generated method stub//((ViewPager) container).removeView(views.get(position));////}@Overridepublic int getItemPosition(Object object) {// TODO Auto-generated method stubreturn super.getItemPosition(object);}//執行個體化@Overridepublic Object instantiateItem(ViewGroup container, int position) {// TODO Auto-generated method stubcontainer.addView(views.get(position));return views.get(position);}//@Override//public Object instantiateItem(View container, int position) {//// TODO Auto-generated method stub//((ViewPager) container).addView(views.get(position));//return views.get(position);//}};viewPager.setAdapter(paAdapter);}/** * 載入要顯示的頁卡 */private void initView() {// TODO Auto-generated method stubLayoutInflater inflater = getLayoutInflater().from(this);view1 = inflater.inflate(R.layout.tv_control, null);view2 = inflater.inflate(R.layout.tv_control_number, null);// 將要分頁顯示的View裝入數組中views = new ArrayList();views.add(view1);views.add(view2);}}在實現adapter的時候一定要重寫以下四個方法,其他方法的話也可以重寫
instantiateItem(ViewGroup container, int position)getItemPosition(Object object)destroyItem(ViewGroup container, int position,Object object)isViewFromObject(View arg0, Object arg1)
需要注意的是布局檔案中需要這樣寫
組件是用來顯示左右滑動的介面的,如果不載入xml布局檔案,他是不會顯示內容的 android:id="@+id/main_viewpager" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" >