【发布时间】:2012-03-26 01:33:31
【问题描述】:
我在我的 viewpager 实现中膨胀视图(而不是片段)。 我的主类扩展了 Activity。
viewpager 的第 1 页扩充了由 textviews 组成的视图。第 2 页膨胀了一个 achartengine 饼图。一切正常。
第 3 页应该使用自定义适配器扩展列表视图。但是,由于我只是扩展了 Activity,所以 setListAdapter 不可用。
如果我将主类更改为扩展 ListActivity,那么整个 Activity 在加载时会失败,因为它找不到 android.R.id.list 视图(因为它还没有被充气)
这是我正在使用的实例化代码
public class SummaryPagerAdapteri extends PagerAdapter{
@Override
public int getCount() {
return 5;
}
@Override
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
View view = null;
switch (position) {
case 0:
//find and inflate the view
resId = R.layout.recordsummary_one;
view = inflater.inflate(resId, null);
//do stuff
break;
case 1:
//find and inflate the view
resId = R.layout.recordsummary_two;
view = inflater.inflate(resId, null);
Context ctx = view.getContext();
//do stuff
break;
case 2:
resId = R.layout.recordsummary_three;
view = inflater.inflate(resId, null);
Context tctx = view.getContext();
if(pdn != null){
adapter = new SummaryNotesAdapter(tctx,pdn);
setListAdapter(adapter);
}
break;
case 3:
resId = R.layout.recordsummary_four;
view = inflater.inflate(resId, null);
//ToDo
break;
case 4:
resId = R.layout.recordsummary_five;
view = inflater.inflate(resId, null);
//ToDo
break;
}
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}
关于如何在不扩展 ListView 的情况下使用 setListAdapter(adapter) 或在首次加载时找不到未膨胀视图时如何扩展 ListView 而不会导致活动失败的任何想法?
非常感谢
戴夫
【问题讨论】:
标签: android