android - NavigationDrawer 不会从右向左移动

标签 android android-fragments navigation-drawer

这是我的代码:

主 Activity

package com.pa.homeautomationblank;

import android.app.Activity;
import android.app.Fragment;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.pa.homeautomationblank.fragments.CenasFragment;


public class MainActivity extends Activity {

    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    private CharSequence mDrawerTitle;
    private CharSequence mTitle;
    private String[] mMenuTitles;

    /* public View getActionBarView() {

        Window window = this.getWindow();
        final View decorView = window.getDecorView();
        final String packageName =  "android";
        final int resId = this.getResources().getIdentifier("action_bar_container", "id", packageName);
        final View actionBarView = decorView.findViewById(resId);
        return actionBarView;
    }

    */

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTitle = mDrawerTitle = getTitle();
        mMenuTitles = getResources().getStringArray(R.array.menu_array);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.dl);
        mDrawerList = (ListView) findViewById(R.id.lvNavigator);

        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
        mDrawerList.setAdapter(new ArrayAdapter<String>(this,R.layout.drawer_list_item, mMenuTitles));

        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());


        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);       


        mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                R.drawable.ic_drawer,  /* nav drawer image to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description for accessibility */
                R.string.drawer_close  /* "close drawer" description for accessibility */
                ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }            

        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);


        if (savedInstanceState == null){
            selectItem(2);
        }

    }


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

    @Override
    public boolean onPrepareOptionsMenu(Menu menu){

        //boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        //menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);             
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onMenuItemSelected(int panel, MenuItem item){

        if (item != null && item.getItemId() == android.R.id.home) {
            if (mDrawerLayout.isDrawerOpen(Gravity.START)) {
                mDrawerLayout.closeDrawer(Gravity.START);
            } else {
                mDrawerLayout.openDrawer(Gravity.START);
            }
        }       
        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.

        // The action bar home/up action should open or close the drawer.
        // ActionBarDrawerToggle will take care of this.
       if (mDrawerToggle.onOptionsItemSelected(item)) {
           return true;
       }
       else {

           int id = item.getItemId();
           if (id == R.id.action_settings) {
               return true;
           }
           return super.onOptionsItemSelected(item);
       }

    }

    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        getActionBar().setTitle(mTitle);
    }

    private void selectItem(int position) {


        // update the main content by replacing fragments

        switch(position){
        case 0:
            break;
        case 1:
            break;
        case 2:
            Fragment cenasFrag = new CenasFragment();
            getFragmentManager().beginTransaction().replace(R.id.dl, cenasFrag).commit();
            mDrawerList.setItemChecked(position, true);
            setTitle(mMenuTitles[position]);
            mDrawerLayout.closeDrawer(mDrawerList);         
            break;
        case 3:
            break;
        case 4:
            break;
        case 5:
            break;

        }

    }

    private class DrawerItemClickListener implements ListView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }

    }

    @Override
    public void onPostCreate(Bundle savedInstanceState){
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggls
        mDrawerToggle.onConfigurationChanged(newConfig);
    }



}

我有以下 XML 来实现 fragment

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#EAEAEA" >

    <RelativeLayout
        android:id="@+id/layout_cenas_bottom"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"

        android:background="#FFFF00" >

        <Button
                android:id="@+id/btnAdicionarCenas"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="@string/title_adicionar_cena"
                android:layout_weight="1"
                android:background="@drawable/mybutton"
                android:textColor="@drawable/mybuttoncolors"
                android:textSize="15sp"
                android:layout_marginBottom="10dp">
            </Button> 

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/layout_cenas_conteudo"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:layout_above="@id/layout_cenas_bottom"
        android:layout_alignParentTop="true"
        android:background="#00FFFF">

    <ListView
        android:id="@+id/lvCenas"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>

    </RelativeLayout>


</RelativeLayout>

和这个 XML 的 Fragment 类:

package com.pa.homeautomationblank.fragments;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.pa.homeautomationblank.R;
import com.pa.homeautomationblank.model.Constantes;

public class CenasFragment extends Fragment {

    public ListView lvCenas;
    private String[] menus = Constantes.menus;

    @Override   
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.fragment_cenas, container,false);

        RelativeLayout rlay = (RelativeLayout) view.findViewById(R.id.layout_cenas_conteudo);

        lvCenas = (ListView) rlay.findViewById(R.id.lvCenas);
        lvCenas.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(view.getContext(), android.R.layout.simple_list_item_1,menus);

        lvCenas.setAdapter(adapter);

        return(view);
    }   


}

我使用此方法设置 fragment :selectItem(int position) 我的问题是: 我可以将 NavigationDrawer 从左向右移动(打开),但是当我使用实现的这个 fragment 时,我无法将 NavigationDrawer 向右移动(关闭)。

我错了什么?

非常感谢!

最佳答案

我解决了我的问题!

在我调用 Fragment 的地方,我在这一行中使用 DrawerLayout 作为 IdContentView:

getFragmentManager().beginTransaction().replace(R.id.dl, dispositivosFrag).commit();

但正确的是:

getFragmentManager().beginTransaction().replace(R.id.flNavigator, dispositivosFrag).commit();

使用 FrameLayout 作为 IdContentView

感谢大家的帮助!

关于android - NavigationDrawer 不会从右向左移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29000461/

相关文章:

android - 单击 map 标记添加 fragment 列表

Android - 可以从右侧抽屉导航吗?

android - 从 Activity 外部调用 findViewById()

android - 如何收听 Android 版 GoogleMap 的平移和缩放

android - 在 ListView 中共享触摸事件

java - 当我使用 getArguments() 时它显示错误(java.lang.NullPointerException)

带有选项卡和 Viewpager 的 Android fragment

java - 同时显示底部导航 View 和抽屉式导航

android - 如何在 Android 中为 Navigation Drawer 添加一个分区分隔符?

android - 如何在android中安装和使用couch db