首先,这是我的 MainPageViewer:
super.onCreate(savedInstanceState);
setContentView(R.layout.viewpager);
MyPageAdapter adapter = new MyPageAdapter();
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
pager.setCurrentItem(0);
我的 PageViewer 的适配器在这里:
public int getCount()
{
return 3;
}
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.blogandbericht;
break;
case 1:
resId = R.layout.blogandbericht;
break;
case 2:
resId = R.layout.blogandbericht;
break;
}
View view = inflater.inflate(resId, null);
((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;
}
(是的,我知道,所有布局都是“bloganbericht”)
这是我的blogandbericht:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/masterSv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#8ad5f0">
<LinearLayout
android:id="@+id/masterLl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="foo bar"/>
</LinearLayout>
</ScrollView>
一切正常。我可以看到每个布局上的三个按钮。但是我怎样才能动态地将文本视图或按钮等视图添加到布局中呢?