我正在尝试第一次构建一个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的空白背景,但没有显示视图
答案 0 :(得分:20)
我想通了 - 这是因为我在isViewFromObject方法中没有任何东西。用
替换nullreturn arg0 == (View) arg1;
解决了这个问题。
答案 1 :(得分: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;
}