Changer la barre d'action avec un fragment lors de la sélection d'un autre onglet, et la barre d'action doit être en haut de l'onglet Bar d'action

StackOverflow https://stackoverflow.com/questions/19850825

Question

! [Lorsque vous appuyez sur l'onglet 1] [1

! [Lorsque appuyez sur l'onglet 2] [2

Lorsque je sélectionne l'onglet Bar d'action, la barre d'action de titre ci-dessus doit être modifiée, mais je ne peux définir qu'une seule barre d'action de titre maintenant. J'ai fatigué ce code ci-dessous. Mais je n'ai pu trouver aucune solution. Quelqu'un peut-il m'aider ???

package com.ActionBar.Tab; 
public class MainActivity extends Activity {
// Declare Tab variables
ActionBar.Tab tab1, tab2, tab3;
TextView tab1TV, tab2TV, tab3TV;

Fragment Fragment1 = new Fragment1();
Fragment Fragment2 = new Fragent2();
Fragment Fragment3 = new Fragment3();

View view1,view2,view3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity__main);

    RelativeLayout custom = new RelativeLayout(getApplicationContext());
    ActionBar.LayoutParams params = new ActionBar.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    custom.setLayoutParams(params); 
    custom.setGravity(Gravity.CENTER);
    ActionBar actionBar = getActionBar();
    actionBar.show();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    LayoutInflater inflater = (LayoutInflater) this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    actionBar.setDisplayShowCustomEnabled(true);

    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    tab1TV = (TextView) findViewById(R.id.tab1Tv);
    tab2TV = (TextView) findViewById(R.id.tab2Tv);
    tab3TV = (TextView) findViewById(R.id.tab3Tv);

    // Create Actionbar Tabs
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // set Tab name
    tab1 = actionBar.newTab().setCustomView(tab1TV);
    tab2 = actionBar.newTab().setCustomView(tab2TV);
    tab3 = actionBar.newTab().setCustomView(tab3TV);

    // set tab listener     
    tab1.setTabListener(new TabListener(tab1Fragment));
    tab2.setTabListener(new TabListener(tab2Fragment));
    tab3.setTabListener(new TabListener(tab3Fragment));

    // Add tabsto the action bar
    actionBar.addTab(tab1);
    actionBar.addTab(tab2);
    actionBar.addTab(tab3);

        int a = R.layout.layout__tab1_action_bar;
        view1 = inflater.inflate(a,null);

        int b=R.layout.layout_tab2_action_bar;
        view2 = inflater.inflate(b,null);

        int c= R.layout.layout_tab3_action_bar;
        view3 = inflater.inflate(c,null);

    actionBar.setCustomView(view1); 
    /*Here the title action bar will change with view1,view2,view3 
    with tab selection but I can only set one action bar*/      
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

Était-ce utile?

La solution

J'ai résolu ce problème. J'ai utilisé le titre de mise en page de la barre d'action dans la méthode Fragments OnResume ().

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top