我在使用 ViewPager 时遇到了一些问题,我使用以下 instantiateItem 方法创建了一个 PagerAdapter
@Override
public Object instantiateItem(View collection, int position) {
View v = in.inflate(R.layout.weeklyoverviewview, null);
ListView lv = (ListView) v.findViewById(R.id.weeklyoverviewlist);
WeekAdapter a = new WeekAdapter(cxt, time.plusWeeks(position).getWeekOfWeekyear(), time.plusWeeks(position).getYear());
lv.setAdapter(a);
TopNavigatorView nv = (TopNavigatorView) v.findViewById(R.id.weeklyoverviewnavigator);
nv.setTitle("W" + time.plusWeeks(position).getWeekOfWeekyear() + " - " + time.plusWeeks(position).getYear());
((ViewPager) collection).addView(v, 0);
return v;
}
WeekAdapter 看起来像
public class WeekAdapter extends BaseAdapter {
public ArrayList<View> views;
public WeekAdapter(Context context, int week, int year) {
LayoutInflater in = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
views = new ArrayList<View>();
DateTime d = new DateTime().withYear(year).withWeekOfWeekyear(week);
for (int i = 0; i < 7; i++) {
View v = in.inflate(R.layout.weeklyoverviewlisttitle, null);
TextView t = (TextView) v.findViewById(R.id.weeklytitletext);
t.setText(d.withDayOfWeek(i + 1).dayOfWeek().getAsText() + ", " + d.withDayOfWeek(i + 1).monthOfYear().getAsText() + " "
+ d.withDayOfWeek(i + 1).getDayOfMonth() + ", " + d.withDayOfWeek(i + 1).getYear());
views.add(v);
}
}
public int getCount() {
return views.size();
}
public Object getItem(int arg0) {
return views.get(arg0);
}
public long getItemId(int arg0) {
return views.get(arg0).getId();
}
public View getView(int arg0, View arg1, ViewGroup arg2) {
return views.get(arg0);
}
}
问题是前 3 页看到直接实例化似乎是正确的,但是当我滑动到另一个视图时,我在列表视图中有 7 个项目,但文本为空。当我用日志检查文本视图中的文本时,我看到文本设置正确,但我没有出现。
编辑:像这样, http: //i50.tinypic.com/108e8ac.png,不能显示为图像 couse 声誉..