2014-12-01 174 views
0

我有一個ViewPager顯示一些佈局。我需要添加兩個按鈕,並以編程方式在所有佈局中顯示相同的按鈕。查看器是帶有ViewPager的簡單XML。我怎樣才能做到這一點?將按鈕添加到ViewPager

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    // View layout = (View) findViewById(R.id.pager); 
    //reply.setLayoutParams(new LayoutParams()); 

    // Inicialización 
    ArrayList<Integer> inte = new ArrayList<Integer>(); 
    viewPager = (ViewPager) findViewById(R.id.pager); 
    //ArrayList<Integer> pageNumber = new ArrayList<Integer>(); 

    actionBar = getActionBar(); 
    for(int i = 0; i < 39; i++) { 
     inte.add(i%3); 
    } 
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager(), inte); 
    viewPager.setAdapter(mAdapter); 
    actionBar.setHomeButtonEnabled(false); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

    // Agrega tabs 
    for(Integer i =0; i<10; i++){ 
     JSONObject aaa = new JSONObject(); 
     try { 
      aaa.put("name", "Página "+(i+1)); 
      aaa.put("type", i%3); 
      System.out.println("name" + " Página "); 
      inte.add(i%3); 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     actionBar.addTab(actionBar.newTab().setText("Página "+(i+1)).setTabListener(this)); 
    } 

回答

2

爲什麼不直接在XML中定義它們,使它們出現在您的ViewPager下面的所有片段?例如:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/palette_bg" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_marginTop="?android:attr/actionBarSize" 
android:orientation="vertical" > 


<com.yourdomain.yourapp.YourCustomViewPager 
    android:id="@+id/pager" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="10"> 
</com.yourdomain.yourapp.YourCustomViewPager> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="2dp" 
    android:layout_marginLeft="2dp" 
    android:layout_marginRight="2dp" 
    android:layout_marginTop="2dp" 
    android:orientation="horizontal" > 

    //Your buttons here. 

</LinearLayout>