2012-07-14 94 views
1

我正在使用ViewPagerExtensions。我想在Pager Adapter類的每個選項卡中開始一個新的活動。在PagerAdapter中開始新活動

眼下這是默認代碼:

@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

我怕你無法做到。

如果你想爲每個頁面不同勢佈局:

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每個選項卡中顯示新的活動

更新

如果y你想開始新的活動。您可以使用ActionBarSherlock

+0

感謝您的回覆。那麼計算器類將不得不以這種格式重新編碼? – input 2012-07-21 21:30:52

+0

看到我更新的答案 – 2012-07-22 03:01:09

0

你是否試圖誇大這些活動的佈局而不是?

​​
+0

我不確定我是否理解。我想在其中一個標籤中顯示日曆。我從這裏有一個類:http://w2davids.wordpress.com/android-simple-calendar我該怎麼做? – input 2012-07-15 14:28:09

+0

@input,你有沒有試過膨脹simple_calendar_view xml佈局文件? – yugidroid 2012-07-15 16:58:50

+0

對不起,我是新手。真的很感謝你的幫助,如果你能告訴我如何做到這一點? – input 2012-07-15 17:24:23