【发布时间】:2011-09-01 23:48:11
【问题描述】:
到目前为止,我一直在整理示例以了解整个 Android SDK 的 API。然而,我陷入了僵局。
我正在尝试使用 XML 文件中的 2 个 TextView 为 LinearLayout 充气。这些将是被分页的视图。我根本无法让它工作,并意识到我完全不明白代码发生了什么。
查看源代码,我可以看到 ViewPager.addNewItem 方法从提供的适配器调用 InstantiateItem。并且 addNewItem 在populate() 中被调用。 populate() 在许多其他地方被调用。
无论如何,在示例中,我已经体验到,当为 PagerAdapter 重写该方法时,必须在从 ViewPager 作为参数传递的 ViewPager 集合上包含对 addView() 的调用。
如果我希望添加多个视图,我认为这将是多次调用 addView() 的情况,但由于 instantiateItem() 将 Object 返回给 ViewPager(在示例中我让它返回它添加的视图)我不知道返回什么。我试过返回膨胀的视图和其他一些东西。
请问,有人能解释一下这里发生了什么吗?
非常感谢。
@Override
public Object instantiateItem(View collection, int position) {
layout = inflater.inflate(R.layout.animallayout, null);
TextView tv = (TextView) layout.findViewById(R.id.textView1);
tv.setText("1________________>" + position);
TextView tv2 = (TextView) layout.findViewById(R.id.textView2);
tv.setText("2________________>" + position);
((ViewPager) collection).addView(layout);
return layout;
}
【问题讨论】:
-
@mAndroid 你能为非母语人士解释这个笑话吗?
标签: android layout android-viewpager