Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android ViewPager使用方法

Android ViewPager使用方法

編輯:關於Android編程

\


<喎?/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; @Override protected 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() { @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0 == arg1; } @Override public int getCount() { // TODO Auto-generated method stub return views.size(); } //銷毀 @Override public void destroyItem(ViewGroup container, int position, Object object) { // TODO Auto-generated method stub container.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)); // // } @Override public int getItemPosition(Object object) { // TODO Auto-generated method stub return super.getItemPosition(object); } //實例化 @Override public Object instantiateItem(ViewGroup container, int position) { // TODO Auto-generated method stub container.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 stub LayoutInflater 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" >
    



						
		
	
				
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved