如何设置导航抽屉开启器和更近?

时间:2014-02-13 02:23:31

标签: android android-fragments android-navigation

我已经实现了带有碎片的导航抽屉,但我似乎把它弄乱了一点。目前,当抽屉关闭而没有选择列表项时,它会重新加载片段。

我的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 个答案:

答案 0 :(得分:1)

点击链接,您可以找到代码中的问题所在。

Android Sliding Menu using Navigation Drawer