2013-01-08 38 views
3

這是我的簡單PagerAdapter和onCreate()分頁活動。對ViewPager的PagerAdapter有誤解。 InstantianteItem方法永遠不會被調用

public class MPagerAdapter extends PagerAdapter{ 

    List<ViewGroup> pages = null; 

    public MPagerAdapter(List<ViewGroup> pages){ 
     this.pages = pages; 
    } 

    @Override 
    public Object instantiateItem(ViewGroup collection, int position){ 
     ViewGroup v = pages.get(position); 
     ((ViewPager) collection).addView(v, 0); 
     return v; 
    } 

    @Override 
    public void destroyItem(ViewGroup collection, int position, Object view){ 
     ((ViewPager) collection).removeView((View) view); 
    } 

    @Override 
    public int getCount(){ 
     return pages.size(); 
    } 

    @Override 
    public boolean isViewFromObject(View view, Object object){ 
     return view.equals(object); 
    } 

    @Override 
    public void finishUpdate(View arg0){ 
    } 

    @Override 
    public void restoreState(Parcelable arg0, ClassLoader arg1){ 
    } 

    @Override 
    public Parcelable saveState(){ 
     return null; 
    } 

    @Override 
    public void startUpdate(View arg0){ 
    } 
} 

這裏是onCreate()的活動,其中尋呼機將生活。我的自定義佈局

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_pager); 
    getActionBar().setDisplayHomeAsUpEnabled(true); 

    Intent intent = getIntent(); 
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); 
    amount=Integer.parseInt(message); 

    Resources res = getResources(); 
    Drawable background = res.getDrawable(R.drawable.page_back1); 

    BackgroudImage bImage = new BackgroudImage(x, y, background); 

    List<MyLayout> pages = new ArrayList<MyLayout>(); 
    int a = amount/bImage.mPointsList.size(); 
    for (int i=0; i<=a; i++){ 
     MyLayout page = new MyLayout(getBaseContext(), bImage);  
     page.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
     pages.add(page); 
    } 
    MPagerAdapter pagerAdapter = new MPagerAdapter(pages); 
    ViewPager viewPager = (ViewPager)findViewById(R.id.pager);//new ViewPager(this); 
    viewPager.setAdapter(pagerAdapter); 
    viewPager.setCurrentItem(1); 
} 

OnLayout()方法,該方法被用作控制pagerView

protected void onLayout(boolean changed, int l, int t, int r, int b) { 
    if (PagerActivity.amount<=points.size()) { 

     for (int i=0; i<PagerActivity.amount; i++){ 
      ImageView childV = new ImageView(super.getContext()); 
      childV.setBackgroundResource(R.drawable.lesson_frame_closed); 
      ViewGroup.LayoutParams imageParams = new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
      childV.setOnClickListener(new View.OnClickListener() { 

        @Override 
        public void onClick(View view) { 

        } 

       }); 
      this.addView(childV, imageParams); 
     } 
     PagerActivity.amount=0; 
    } 

    if ((float)(bgrImage.getBackground().getIntrinsicWidth())/(float)(bgrImage.getBackground().getIntrinsicHeight())<(float)this.getMeasuredWidth()/(float)this.getMeasuredHeight()) { 
     scaleFX=(float)this.getMeasuredHeight()/(float)bgrImage.getBackground().getIntrinsicWidth(); 
     scaleFY=(float)this.getMeasuredHeight()/(float)bgrImage.getBackground().getIntrinsicHeight(); 
    } else { 
     scaleFX=(float)this.getMeasuredWidth()/(float)bgrImage.getBackground().getIntrinsicWidth(); 
     scaleFY=(float)this.getMeasuredWidth()/(float)bgrImage.getBackground().getIntrinsicHeight(); 
    } 

    for (int i = 0; i < getChildCount(); i++) { 
     final View childV = getChildAt(i); 
     if (childV.getVisibility() != View.VISIBLE) 
     continue; 
     childV.layout((int)(points.get(i).getOriginX()*scaleFX), (int)(points.get(i).getOriginY()*scaleFY), childV.getWidth(), childV.getHeight()); 
    } 

} 

的頁面不明白爲什麼InstantiateItem()不會被調用。任何有用的想法將不勝感激。

回答

0

ViewPagerView,應該添加到UI中。通常通過在佈局文件中聲明它。

從SDK樣本中提取:

  • 佈局文件:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:gravity="center_horizontal" 
        android:orientation="vertical" 
        android:padding="4dip" > 
    
        <android.support.v4.view.ViewPager 
         android:id="@+id/pager" 
         android:layout_width="match_parent" 
         android:layout_height="0px" 
         android:layout_weight="1" > 
        </android.support.v4.view.ViewPager> 
        ... 
    
  • 活動類

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.fragment_pager); 
    
        mAdapter = new MyAdapter(getSupportFragmentManager()); 
    
        mPager = (ViewPager)findViewById(R.id.pager); 
        mPager.setAdapter(mAdapter); 
        ... 
    

完整的來源是在演員/安卓/支持/ SAMPL es/Support4Demos目錄下的SDK

+0

ViewPager是一個視圖列表。它不能在佈局文件中聲明,pager(視圖)頁面必須聲明並在我的項目中聲明。但我不明白爲什麼InstantiateItem沒有被調用 –

+0

應該聲明'ViewPager',**不** **頁面:viewpager要求適配器在需要時創建它們。基本上它的作用就像一個'ListView':你聲明'ListView',然後將子創建委託給適配器。 – bwt

+0

SDK中的Support4Demos示例項目中有示例。 – bwt

相關問題