2013-03-03 46 views
0

我有一個viewpager,我正在使用它作爲雜誌電子閱讀器應用程序中的主要導航元素。我已經定義了一個佈局來充當每個文章的「封面」,並且這些顯示都很好。不過,我想通過膨脹不同的佈局來每6次顯示廣告。所以,在viewpager適配器的instantiateItem方法上,我有以下語句:if ((position != 0) && (position%6 == 0) && (adsShown < ads.size()))。該計劃啓動罰款和文章的意見充分適當。但是,當我到達應顯示廣告的位置時,視圖是空白的。爲什麼是這樣?爲什麼我的viewpager返回空白視圖?

下面是instantiateItem方法的完整代碼:

 @Override 
    public Object instantiateItem(View arg0, int position) { 

     if ((position != 0) && (position%2 == 0) && (adsShown < ads.size())){ 
      View v = getLayoutInflater().inflate(R.layout.adview, null); 

      ImageView adImg = (ImageView)v.findViewById(R.id.adImage); 
      adImg.setImageBitmap(ads.get(adsShown).image); 
      v.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 

        Intent intent = new Intent(); 
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(ads.get(adsShown).linkURL)); 
        startActivity(browserIntent); 
       } 
      }); 
      adsShown++;   
      System.out.println("ADSSHOWN" + adsShown); 
      return v; 
     } 

     else{ 

    Article s = toShow.get(position-adsShown); 

    View v = getLayoutInflater().inflate(R.layout.previewpage, null); 

    TextView hl = (TextView)v.findViewById(R.id.coverHl); 
    ImageView img = (ImageView)v.findViewById(R.id.coverImg); 
    TextView blurb = (TextView)v.findViewById(R.id.coverBlurb); 
    TextView author = (TextView)v.findViewById(R.id.coverAuthor); 
    TextView bot = (TextView)v.findViewById(R.id.coverBot); 



    hl.setText(s.title); 
    img.setImageBitmap(s.image); 
    blurb.setText(Html.fromHtml(s.excerpt)); 
    author.setText("by "+ s.author.name);   
    bot.setText(s.cats.get(0).name + " // " + s.date); 

    v.setOnClickListener(new OnClickListener() { 


     @Override 
     public void onClick(View v) { 

      Intent intent = new Intent(); 
      Article topass = showing; 
      // System.out.println("XXYYZZAA: " + topass.headline); 
      intent.setClassName("com.calib.caliber", "com.calib.caliber.ArticleView"); 
    //  intent.putExtra("title", topass.title); 
      intent.putExtra("a", topass); 
    //  intent.putExtra("author", topass.author.name); 
     // intent.putExtra("content", topass.content); 
    //  intent.putExtra("id", topass.ID); 

      startActivityForResult(intent, 1);    
     } 
    }); 


    ((ViewPager) arg0).addView(v); 

    return v; 
     } 
} 

,這裏是爲AD瀏覽活動的XML佈局文件:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#000000"> 

<TextView 
    android:id = "@+id/adText" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text = "advertisement" 
    android:textColor="#FFFFFF" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true"></TextView> 

<ImageView 
    android:id = "@+id/adImage" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"  
    android:layout_below = "@id/adText"> 
    </ImageView> 

</RelativeLayout> 

回答

2

我認爲你從忘了((ViewPager) arg0).addView(v);如果()第一部分,而其他部分包含它。

+0

哎呀,謝謝你指出。 – drewmoore 2013-03-03 22:54:25

相關問題