45

我正在开发一个 android 项目,我正在尝试使用http://developer.android.com/training/implementing-navigation/nav-drawer.html中的示例集成新的 Navigation Drawer 。

它主要工作除了一件事,操作栏向上按钮不显示菜单,但如果我从边缘滑动手指在主活动屏幕上,然后菜单出现,所以我知道实际菜单没有问题,它只是操作栏按钮。

下面是代码

public class MainActivity extends Activity {

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

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTitle = mDrawerTitle = getTitle();
        mPlanetTitles = getResources().getStringArray(R.array.planets_array);

        mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
        mDrawerList = (ListView)findViewById(R.id.left_drawer);


        MenuItemAdapter menuAdapter = new MenuItemAdapter(this);

        menuAdapter.add(new MenuItem("Hello"));
        menuAdapter.add(new MenuItem("World"));
        menuAdapter.add(new MenuItem("Parsnips"));
        menuAdapter.add(new MenuItem("Turnips"));

        mDrawerList.setAdapter(menuAdapter);

        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

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

        mDrawerToggle = new ActionBarDrawerToggle(
                this,
                mDrawerLayout,
                R.drawable.ic_drawer, 
                R.string.drawer_open,
                R.string.drawer_closed)
        {
            public void onDrawerClosed(View view)
            {
                getActionBar().setTitle(mTitle);
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView)
            {
                getActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu();
            }
        };

        mDrawerLayout.setDrawerListener(mDrawerToggle);
        if (savedInstanceState ==  null)
        {
            selectItem(0);
        }
    }

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

    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }
    }

    private void selectItem(int position) {
        // update the main content by replacing fragments

        android.app.Fragment fragment = new PlanetFragment();
        Bundle args = new Bundle();
        args.putInt(PlanetFragment.ARGS_PLANET_NUMBER, position);
        fragment.setArguments(args);

        android.app.FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

        mDrawerList.setItemChecked(position, true);
        setTitle(mPlanetTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    }

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

    @Override
    protected void onPostCreate(Bundle savedInstanceState)
    {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig)
    {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    public static class PlanetFragment extends android.app.Fragment
    {
        public static final String ARGS_PLANET_NUMBER = "planet_number";

        public PlanetFragment()
        {

        }

        @Override
        public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle savedInstanceState)
        {
            View rootView = inflator.inflate(R.layout.fragment_planet, container, false);
            int i = getArguments().getInt(ARGS_PLANET_NUMBER);
            String planet = getResources().getStringArray(R.array.planets_array)[i];

            //((TextView)rootView.findViewById(R.id.fragment_text_view)).setText(planet);
            getActivity().setTitle("Planet: " + planet);
            return rootView;
        }
    }

    private class MenuItem
    {
        public String menuName;

        public MenuItem(String menuName)
        {
            this.menuName = menuName;
        }
    }

    public class MenuItemAdapter extends ArrayAdapter<MenuItem>
    {
        public MenuItemAdapter (Context context)
        {
            super(context,0);
        }

        public View getView (int position, View convertView, ViewGroup parent)
        {
            convertView = null;
            if (convertView == null)
            {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_menu_item, null);
            }

            /*TextView title = (TextView)convertView.findViewById(R.id.row_title);
            title.setText(getItem(position).menuName);
            title.setVisibility(View.VISIBLE);*/

            EditText title = (EditText)convertView.findViewById(R.id.row_edittext);
            title.setHint(getItem(position).menuName);
            title.setVisibility(View.VISIBLE);

            return convertView;
        }
    }
}

感谢您的任何帮助,您可以提供。

4

4 回答 4

163

你忘了实现 onOptionsItemSelected

这就是魔法发生的地方:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Pass the event to ActionBarDrawerToggle, if it returns
        // true, then it has handled the app icon touch event
        if (mDrawerToggle.onOptionsItemSelected(item)) {
          return true;
        }
        // Handle your other action bar items...

        return super.onOptionsItemSelected(item);
    }
于 2013-05-24T23:45:51.037 回答
6

对于那些仍然有问题的人,你可能会错过这个方法(OP有但我已经删除了):

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}
于 2016-02-04T04:15:47.357 回答
2

它为我工作。

drawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mDrawer.openDrawer(GravityCompat.START);
        }
    });
于 2016-06-20T15:59:03.143 回答
0

在这里,我将告诉您在不使用 android studio 的情况下在 android 中创建抽屉导航的简单方法。我刚刚使用 ADT 创建导航抽屉。这是代码看看

activity_main.xml

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dr_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout 
        android:id="@+id/mainContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
     <TextView 
        android:id="@+id/txt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="swipe content"/>
     <Button 
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text = "Click to open d"/>
   </RelativeLayout>
   <RelativeLayout 
        android:id="@+id/drawer"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_gravity = "start"
        android:background="#FFFFFF">
    <TextView 
         android:id="@+id/txt2"
         android:layout_width="200dp"
         android:layout_height="wrap_content"
         android:text="drawer content are here arr"/>
    <Button 
         android:id="@+id/bt2"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text = "Click to open d"/>
   </RelativeLayout>

</android.support.v4.widget.DrawerLayout>

MainActivity.java

package com.example.drawer1;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.res.Configuration;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.DrawerLayout.DrawerListener; 
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
    private DrawerLayout drawerLayout;
    private View drawerView;
    Button bt1,bt2;
    private DrawerListener myDrawerListner;
    private ActionBarDrawerToggle mDrawerTogle;
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drawerLayout = (DrawerLayout) findViewById(R.id.dr_layout);
        drawerView = (View) findViewById(R.id.drawer);
        drawerLayout.setDrawerListener(myDrawerListner);
        bt1 = (Button) findViewById(R.id.bt1);
        bt2 = (Button) findViewById(R.id.bt2);
        mDrawerTogle =new ActionBarDrawerToggle(this, drawerLayout,  
        R.drawable.ic_drawer,R.string.open_drawer,R.string.close_drawer){
            public void onDrawerOpened(View drawerView) {
            // TODO Auto-generated method stub
                super.onDrawerOpened(drawerView);
                getActionBar().setTitle("SpeakEng");
            }
            public void onDrawerClosed(View view) {
            // TODO Auto-generated method stub
                super.onDrawerClosed(view);
                getActionBar().setTitle("SpeakEng");
            }
        };
        drawerLayout.setDrawerListener(mDrawerTogle);

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

        bt2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                   drawerLayout.closeDrawer(drawerView);
            }
        });

        bt1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                   drawerLayout.openDrawer(drawerView);

            }
        });
    }
    @Override
    public void onConfigurationChanged(Configuration newConfig){
        super.onConfigurationChanged(newConfig);
        mDrawerTogle.onConfigurationChanged(newConfig);
    }
    public boolean onOptionsItemSelected(MenuItem item){
        if (mDrawerTogle.onOptionsItemSelected(item)){
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    @Override
    protected void onPostCreate(Bundle savedInstanceState){
        super.onPostCreate(savedInstanceState);
        //Sync the toogle state after onRestoreInstanceState has occured.
        mDrawerTogle.syncState();
    }
   }

您可以打开抽屉 1)通过单击按钮,2)从左侧拉动,3)单击操作栏上的抽屉图标。您可以根据需要打开它。我给了您三个选项。

注意:drawerLayout 必须是根元素,如代码所示。并保持 onConfigurationChanged()、onOptionsItemSelected()、onPostCreate()。这三个方法对于创建抽屉式导航非常重要。

祝你好运!

于 2017-07-04T12:21:54.133 回答