2014-02-13 49 views
0

我已經實現了一個帶有碎片的導航抽屜,但我似乎已經把它搞砸了一點。目前每當抽屜關閉而沒有選擇listitem時,它會重新加載一個片段。如何設置導航抽屜開啓器和更近?

持有的片段我MainDrawer類是:

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(); 

    } 


} 

回答

1

按照鏈接,你可以找出是在你的代碼的問題。

Android Sliding Menu using Navigation Drawer

+0

真棒,因此我的代碼有點改變了。看起來不錯,但導航抽屜加載新片段的onCLicks似乎沒有工作: http://stackoverflow.com/questions/21760094/onclick-for-navigationdrawer-not-loading-new-fragments – Mike