在PagerAdapter中开始新活动

时间:2012-07-14 18:47:51

标签: java android tabs android-viewpager

我正在使用ViewPagerExtensions。我想在Pager Adapter类的每个选项卡中启动一个新活动。

现在这是默认代码:

@Override
public Object instantiateItem(ViewGroup 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;

}

我想为android标签做一些事情:

@Override
public Object instantiateItem(View container, int position) {

    // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, NewActivity.class);

}

这会引发错误:The method setClass(Context, Class<?>) in the type Intent is not applicable for the arguments (PagerAdapter, Class<NewActivity>)

2 个答案:

答案 0 :(得分:2)

我害怕你无法做到。

如果您想为每个页面设置不同的布局:

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;
}

我认为您无法使用ViewPagerExtensions

在每个标签中显示新活动

<强>更新

如果您想开始新的活动。您可以使用ActionBarSherlock

答案 1 :(得分:0)

您是否尝试过膨胀这些活动的布局而不是?

@Override
public Object instantiateItem(View collection, int position) {
    View layout = inflater.inflate(R.layout.activity_layout, null);

    /** Find your layout Views. */
    TextView text= (TextView) layout.findViewById(R.id.any_id);

    // Set some values to your Views.

    ((ViewPager) collection).addView(layout);

    return layout;
}