我想在instantiateItem()函数下启动新活动。我对这个函数的理解是它只允许在这个函数下添加TextView。是否可以在功能下创建新的活动或意图?代码描述如下。
@Override
public Object instantiateItem(View container, int position) {
RelativeLayout v = new RelativeLayout(mContext);
TextView t = new TextView(mContext);
t.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
t.setText(mData[position]);
t.setTextSize(30);
t.setGravity(Gravity.CENTER);
v.addView(t);
((ViewPager) container).addView(v, 0);
return v;
}
答案 0 :(得分:1)
如果您希望每个页面都有不同的布局,那么您可以在instantiateItem
View parent_view = null;
if (position == 0) {
parent_view = getViewForPageOne();
} else if (position == 1) {
parent_view = getViewForPageTwo();
.........
((ViewPager) collection).addView(parent_view, 0);
return parent_view;
}
和
private View getViewForPageOne(){
View v = getLayoutInflater().inflate(R.layout.layout_page_one, null);
TextView whatText =(TextView) v.findViewById(R.id.idOfTextView);
whatText.setText("Page One");
....
....
return v;
}