4

我第一次尝试构建一个viewpager。我在主 onCreate 方法中填充视图的 ArrayList 并将其传递给 PagerAdapter 的构造函数。活动启动正常,我可以看到 ViewPager(或至少它的背景),但没有任何视图出现在其中,我只看到空白背景。我试过膨胀我知道在其他小部件中工作的布局 XML,所以那里不会有问题。我猜这与我编写 instantiateObject 方法的方式有关。无论如何,这是我的适配器:

    public class StoryAdapter extends PagerAdapter {
    private Context ctx;
    private ViewBuilder viewBuilder;

    public StoryAdapter(Context ctx, ArrayList<View> views){
        this.ctx = ctx;
    }


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

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public Object instantiateItem(View arg0, int arg1) {
            View v = views.get(arg1);
            ((ViewPager) arg0).addView(v);
            return v;
            }

    @Override
    public void destroyItem(View pager, int arg1, Object view) {
    ((ViewPager) pager).removeView((View) view);

}
}

这是我的 onCreate():

public class ShowStory extends Activity {
public int totalPages;
Button backBtn;
public String text;
public ArrayList<View> views;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_story);

    backBtn = (Button)findViewById(R.id.backBtn);

    backBtn.setOnClickListener(new OnClickListener() {
        public void onClick(View view){
            Intent intent = new Intent();
            setResult(RESULT_CANCELED, intent);        
            finish();       
        }
    });
    ViewPager vp = (ViewPager)findViewById(R.id.storyFlipper);
    views = new ArrayList<View>();

    View v0 = getLayoutInflater().inflate(R.layout.galleryview, null);

    TextView v0t = (TextView)v0.findViewById(R.id.headline);
    v0t.setText("COVER1");

    View v1 = getLayoutInflater().inflate(R.layout.coverpage, null);
//      TextView v1t = (TextView)findViewById(R.id.coverText);
//      v1t.setText("COVER2");      

    View v2 = getLayoutInflater().inflate(R.layout.coverpage, null);
//      TextView v2t = (TextView)findViewById(R.id.coverText);
//      v2t.setText("COVER3");  

views.add(v0);
views.add(v1);
views.add(v2);  


    StoryAdapter sa = new StoryAdapter(this, views);        
    vp.setAdapter(sa);

PS我这样做主要是为了教育目的,这显然是一个非常简单的例子。任何帮助将不胜感激!

------编辑:我尝试了您的建议:

        @Override
    public Object instantiateItem(View collection, int position) {
    LayoutInflater inflater = (LayoutInflater) collection.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    int resId = 0;
    switch (position) {

    case 0:
        resId = R.layout.coverpage;
        break;
    case 1:
        resId = R.layout.coverpage;
        break;
    case 2:
        resId = R.layout.coverpage;
        break;
    case 3:
        resId = R.layout.coverpage;
        break;

    }
    View view = inflater.inflate(resId, null);
    ((ViewPager) collection).addView(view, 0);
    return view;

    }

并得到相同的结果 - 我看到 viewpager 的空白背景,但没有出现任何视图

4

2 回答 2

20

我想通了 - 这是因为我在 isViewFromObject 方法中没有任何内容。将 null 替换为

return arg0 == (View) arg1;

解决了这个问题。

于 2013-01-21T03:25:00.013 回答
0

我认为视图需要在 instantiateItems 方法中膨胀。我是这样做的:

@Override
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

int resId = 0;
switch (position) {

case 0:
    resId = R.layout.first_layout_id;

    break;
case 1:
    resId = R.layout.layout_id;
    break;
case 2:
    resId = R.layout.layout_id;
    break;
case 3:
    resId = R.layout.layout_id;
    break;

}

View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;

}
于 2013-01-21T03:05:26.610 回答