Jump to content

ActionBar avec Tab + Activity


gogui63

Recommended Posts

Bonjour, j'ai une application avec deux onglets, dans le premier une listFragment qui fonctionne bien, chaque item lance une activité.

Par contre j'aimerais que lorsque je clique dans le deuxième onglet afficher une activity mais il faut que les Tab restent affichés, voila mon code : 

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {


    private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);


        // For each of the sections in the app, add a tab to the action bar.
        actionBar.addTab(actionBar.newTab().setText("Tab 1").setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText("Tab 2").setTabListener(this));
    }


    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
            getActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
        }
    }


    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar().getSelectedNavigationIndex());
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.Apropos:
                Intent myIntent = new Intent(this,APropos.class);
                startActivity(myIntent);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }






    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }


    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {


        /**
         * On first tab we will show our list
         */
        if (tab.getPosition() == 0) {
            Acceuil simpleListFragment = new Acceuil();
            getSupportFragmentManager().beginTransaction().replace(R.id.container, simpleListFragment).commit();
        }
//ICI L'ACTIVITY SE LANCE BIEN MAIS CACHE LES ONGLETS, QUAND JE CLIQUE SUR RETOUR LE CURSEUR SE PLACE SUR L ONGLET 2 
//MAIS AFFICHE MA LISTE ( CELLE DE L ONGLET 1 ) 
        else if (tab.getPosition() == 1) {
            Intent intent = new Intent(this, Activity2.class);
            startActivity(intent);
        }

    }


    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }
}

Merci d'avance.

Edited by gogui63
Link to comment
Share on other sites

Bonjour,

 

Tu devrais gérer le contenu de ton deuxième onglet avec un Fragment, comme pour le premier, si tu veux rester sur l'Activity avec les onglets. Parce que là, tu demandes explicitement d'afficher une nouvelle Activity

Link to comment
Share on other sites

OK, merci.

Par contre je n'ai pas saisi comment on fait un "new" du genre : 

 public void addListenerOnButton() {


        btnlent1 = (ImageButton) getView().findViewById(R.id.imglent1);
        btnlent1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {


                AlertDialog.Builder alert = new AlertDialog.Builder(DummySectionFragment.this);


                alert.setTitle("Title");

                WebView wv = new WebView(DummySectionFragment.this);
                wv.getSettings().setJavaScriptEnabled(true);
                wv.loadUrl("file:///android_res/drawable/image.jpg");
                wv.setWebViewClient(new WebViewClient() {
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view,
                                                            String url) {
                        view.loadUrl(url);

                        return true;
                    }
                });

                alert.setNegativeButton("OK",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int id) {
                            }
                        });
                Dialog d = alert.setView(wv).create();
                d.show();

            }


        });

Edited by gogui63
Link to comment
Share on other sites

Il faut sans doute que tu fasses un:

AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());

car tu es maintenant dans un Fragment, et le builder attend une Activity (ou plus exactement un Context, ce que n'est pas un Fragment)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...