导航抽屉没有关闭

时间:2015-05-29 16:40:46

标签: android navigation-drawer

我在android中实现了一个导航器。我不明白当抽屉列表中的一个项目被选中时如何关闭抽屉。这个代码给了我一个空指针异常。 主要活动:

public class MainActivity extends ActionBarActivity {

    String[] planet={"earth","mars","jupiter"};
    ListView lv;
    DrawerLayout mDrawerLayout;
    NavigationDrawerFragment navigationDrawerFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if(savedInstanceState==null){
             earth earthfrag=new earth();
            FragmentManager fragmentManager=getSupportFragmentManager();
            FragmentTransaction transaction=fragmentManager.beginTransaction();
            transaction.add(R.id.mainactivity,earthfrag,"earth").commit();

        }
        Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);
        mDrawerLayout=(DrawerLayout)findViewById(R.id.nav_drawer);
        navigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.drawer_fragment);
        navigationDrawerFragment.setup(mDrawerLayout, toolbar);
        lv= (ListView) findViewById(R.id.listView);
        lv.setAdapter(new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,planet));
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                FragmentManager fragmentManager=getSupportFragmentManager();
                FragmentTransaction transaction=fragmentManager.beginTransaction();
                    if(position==2){
                        jupiter jupiterfrag=new jupiter();
                        transaction.replace(R.id.mainactivity, jupiterfrag, "jupiter").commit();
                    }
                if (position==1){
                    mars marsfrag=new mars();
                    transaction.replace(R.id.mainactivity,marsfrag,"mars").commit();
                }
                if (position==0){
                    earth earthfrag=new earth();
                    transaction.replace(R.id.mainactivity,earthfrag,"earth").commit();
                }
    mDrawerLayout.closeDrawer(lv);
            }
        });
    }


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

NavigationDrawerFragment:

public class NavigationDrawerFragment extends Fragment {

    private ActionBarDrawerToggle mActionBarDrawerToggle;
    private  DrawerLayout mDrawerLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        return inflater.inflate(R.layout.drawer_fragment,container,false);
    }

    public void setup(DrawerLayout drawerLayout,Toolbar toolbar) {
        mDrawerLayout=drawerLayout;
        mActionBarDrawerToggle=new ActionBarDrawerToggle(getActivity(),mDrawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close){
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }
        };
        mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
        mDrawerLayout.post(new Runnable() {
            @Override
            public void run() {
               mActionBarDrawerToggle.syncState();
            }
        });
    }
}

activity_main.xml:

<?xml version="1.0"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
        <include
            android:id="@+id/app_bar"
            layout="@layout/app_bar" />
        <RelativeLayout
            android:id="@+id/mainactivity"
            android:layout_width="match_parent"
            android:layout_height="match_parent"></RelativeLayout>
    </LinearLayout>
    <fragment
        android:id="@+id/drawer_fragment"
        android:name="com.example.joy.ecoapp.NavigationDrawerFragment"
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        tools:layout="@layout/drawer_fragment" />
</android.support.v4.widget.DrawerLayout>

1 个答案:

答案 0 :(得分:0)

我相信你需要将片段包装在一个容器中,例如一个LinearLayout。然后使用该容器作为抽屉:

<LinearLayout
    android:id="@+id/lv_wrapper"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@android:color/background_light"
    android:padding="5dp" >
    <fragment 
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:id="@+id/drawer_fragment"
        tools:layout="@layout/drawer_fragment"
        android:name="com.example.joy.ecoapp.NavigationDrawerFragment" />
</LinearLayout>

然后在你的代码中而不是调用

mDrawerLayout.closeDrawer(lv);

用你的新包装器调用它:

mDrawerLayout.closeDrawer(findViewById(R.id.lv_wrapper);