Post Reply

Nav drawer reloads fragments each time it closes...

OP searayman

13th February 2014, 03:12 AM   |  #1  
OP Junior Member
Thanks Meter: 0
 
7 posts
Join Date:Joined: Jun 2013
I have a navigationDrawer that I am implementing for the first time, and each time I close the navigationDrawer without selecting an option in the listVIew it reloads a fragment.... I need help on how to reorganize my code so the drawer can close without reloading the fragments each time.

Code:
public class MainDraw extends FragmentActivity {
    final String[] data ={"Statistics","Discover","three"};
    final String[] fragments ={
            "com.beerportfolio.beerportfoliopro.StatisticsPage",
            "com.beerportfolio.beerportfoliopro.Discover",
            "com.beerportfolio.beerportfoliopro.FragmentThree"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);


        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, data);

        final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);


        final ListView navList = (ListView) findViewById(R.id.drawer);
        navList.setAdapter(adapter);

        navList.setOnItemClickListener(new AdapterView.OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
                drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){



                    @Override
                    public void onDrawerClosed(View drawerView){
                        super.onDrawerClosed(drawerView);
                        FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
                        tx.replace(R.id.main, Fragment.instantiate(MainDraw.this, fragments[pos]));
                        tx.commit();


                    }

                });

                drawer.closeDrawer(navList);

            }
        });

        FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
        tx.replace(R.id.main,Fragment.instantiate(MainDraw.this, fragments[0]));
        tx.commit();

    }


}
Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Top Threads in Java for Android App Development by ThreadRank