导航抽屉不会从右向左移动

时间:2015-03-12 01:29:49

标签: android android-fragments navigation-drawer

这是我的代码:

MainActivity

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来实现片段

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


}

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

我错了什么?

非常感谢!

3 个答案:

答案 0 :(得分:1)

我解决了我的问题!

我在呼叫Fragment的时候,我在此行中使用DrawerLayout作为IdContentView

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

但正确的是:

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

FrameLayout用作IdContentView

感谢您的帮助!

答案 1 :(得分:0)

使用此

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) { 
@Override
 public boolean onOptionsItemSelected(MenuItem item) {
 if (item != null && item.getItemId() == android.R.id.home) { 
if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) { 
mDrawerLayout.closeDrawer(Gravity.RIGHT); 
} else { 
mDrawerLayout.openDrawer(Gravity.RIGHT);
 }
 } 
return false; 
} 
};

并将listview gravity设置为右侧

android:layout_gravity="right

答案 2 :(得分:0)

您不是在此代码中显示mainactivity xml
并尝试此链接link here