2012-10-22 154 views
0

我正在做一個家庭替代應用程序,我使用viewpager有三個3橫向導航佈局。問題是這些佈局存儲在三個不同的佈局資源文件(layout1.xml,layout2.xml和layout3.xml)中,而不是他們,我想動態地爲每個屏幕創建內容。以編程方式創建佈局以添加到ViewPager?

這是規定每一頁的內容Adapater的摘錄:

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.layout1; 
      break; 
     case 1: 
      resId = R.layout.layout2; 
      break; 
     case 2: 
      resId = R.layout.layout3; 
      break; 
     } 
     View view = inflater.inflate(resId, null); 
     ((ViewPager) collection).addView(view, 0); 
     return view; 
    } 

我如何編程方式創建一個簡單的佈局,並把它放在我的屏幕之一?我一直在做這方面的工作,找不到任何有關如何做到這一點的線索,任何指向正確方向的信息都會很有用。


編輯:

這就是我現在想,我不認爲我是很離譜,但所有屏幕都是空白:

Context context = collection.getContext(); 
LinearLayout layout = new LinearLayout(context); 
TextView view = new TextView(context); 
switch (position) { 
    case 0: 
     view.setText("some text"); 
     view.setPadding(5, 5, 5, 5); 
     view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
     break; 
    case 1: 
     view.setText("some text"); 
     view.setPadding(5, 5, 5, 5); 
     view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
     break; 
    case 2: 
     view.setText("some text"); 
     view.setPadding(5, 5, 5, 5); 
     view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
     break; 
} 
layout.addView(view); 
return layout; 

回答

1

充氣佈局而不是,創建佈局和視圖的對象並將其返回。

示例位置1,

LinearLayout layout = new LinearLayout(context); 
switch (position) { 
     case 0: 
      TextView tv = new TextView(context); 
      tv.setText("some text"); 
      tv.setPadding(5, 5, 5, 5); 
      tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
      // And so on customize and create as many views as you want. 

      // And finally add them to your layout 
      layout.addView(tv); 
      return layout; 

     case 1: 
      . 
      . 
      . 
     case 2: 
      . 
      . 
      . 
} 

UPDATE:

問題上的更新後編輯答案。

我已經採取了您的代碼並對其進行了修改。這一定是有效的。

@Override 
public Object instantiateItem(View container, int position) { 
    Context context = collection.getContext(); 
    LinearLayout layout = new LinearLayout(context); 
    TextView view = new TextView(context); 

    switch (position) { 

    case 0: 
     view.setText("some text"); 
     view.setPadding(5, 5, 5, 5); 
     view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
     break; 
    case 1: 
     view.setText("some text"); 
     view.setPadding(5, 5, 5, 5); 
     view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
     break; 
    case 2: 
     view.setText("some text"); 
     view.setPadding(5, 5, 5, 5); 
     view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
     break; 
    } 
    layout.addView(view); 
    ((ViewPager) container).addView(layout, 0); // This is the line I added 
    return layout; 
} 
+0

我只是試圖實現它,應用程序不會崩潰,但我沒有看到每個屏幕上的文本(所有屏幕都是空白的)。我編輯了這個問題,以顯示我的嘗試。你能否看到我是否犯了一些錯誤? – lisovaccaro

+0

我在閱讀完編輯後更新了答案。請檢查。 – Enigma

+0

謝謝,它似乎工作。只是一個問題:當我在第二行使用這段代碼時,出現錯誤「集合無法解析」,我將它更改爲容器,因爲它看起來應該與方法中傳遞的參數匹配。這個可以嗎?一切似乎工作正常,但我想問 – lisovaccaro

相關問題