0

我有一个浏览器,用作杂志电子阅读器应用程序中的主要导航元素。我已经为每篇文章定义了一个扩展为“封面页”的布局,这些都显示得很好。但是,我想增加一个不同的布局,以在每 6 次查看时显示一个广告。所以,在 viewpager 适配器的 instantiateItem 方法上,我有以下语句if ((position != 0) && (position%6 == 0) && (adsShown < ads.size())):该程序启动良好,并且文章视图正确膨胀。但是,当我到达应该显示广告的位置时,视图是空白的。为什么是这样?

这是 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;
        }
}

这是 adview 活动的 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>
4

1 回答 1

2

我认为您忘记了((ViewPager) arg0).addView(v);if() 第一部分中的 if () 而 else 包含它。

于 2013-03-03T22:47:00.210 回答