切换按钮不适用于导航抽屉

时间:2014-04-24 15:27:24

标签: android navigation-drawer togglebutton

我正在为我的Android应用程序实现导航抽屉。我通过从侧面拖动来使抽屉工作,但操作栏上的主页/切换按钮不会更改为我想要的图标并且不响应点击,您在其中定义的onDrawerClosed和onDrawerOpened方法被调用。 我的代码如下:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/drawer_layout"
     android:layout_width="match_parent"
     android:layout_height="match_parent">


    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/loadingFragment"
        android:name="com.whostr.clients.android.LoadingFragment" />

    <fragment 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/eventFragment"
        android:name="com.whostr.clients.android.EventDisplayFragment"/>

    <fragment 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/emptyFragment"
        android:name="com.whostr.clients.android.EmptyView"/>



<!--  Listview to display slider menu -->
<ListView android:id="@+id/left_drawer"
    android:layout_width = "240dp"
    android:layout_height = "match_parent"
    android:layout_gravity = "start"
    android:choiceMode = "singleChoice"
    android:dividerHeight = "1dp"
    android:divider = "@android:color/transparent"
    android:background="#111"
    />

在活动中,我有以下代码:

    @Override
public void onCreate(Bundle savedDataInstace)
{
    super.onCreate(savedDataInstace);
    setContentView(R.layout.eventdisplay);

    fm = getSupportFragmentManager();
    //get fragments by id
    fragments[EVENT_VIEW]= fm.findFragmentById(R.id.eventFragment);
    fragments[LOADING] = fm.findFragmentById(R.id.loadingFragment);
    fragments[EMPTY_VIEW] = fm.findFragmentById(R.id.emptyFragment);


    menuList = getResources().getStringArray(R.array.menuArray);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawwer_open, R.string.drawer_closed)
                        {
                            /*
                             * Called when a drawer has settled in a completely closed state
                             */
                            public void onDrawerClosed(View view)
                            {
                                Log.d("drawerToggle", "Drawer closed");
                                super.onDrawerClosed(view);
                                getActionBar().setTitle(R.string.app_name);
                                invalidateOptionsMenu(); //Creates call to onPrepareOptionsMenu()
                            }

                            /*
                             * Called when a drawer has settled in a completely open state
                             */
                            public void onDrawerOpened(View drawerView)
                            {
                                Log.d("drawerToggle", "Drawer opened");
                                super.onDrawerOpened(drawerView);
                                getActionBar().setTitle("NavigationDrawer");
                                invalidateOptionsMenu();
                            }
                        };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    //Set the adapter for the list view
    mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, menuList));
    mDrawerList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int position,
                long id) {
            Log.d("navigationDrawer", "Clicked on an item");
        }

    });


    FragmentTransaction transaction = fm.beginTransaction();

    //Hide the event view
    transaction.hide(fragments[EVENT_VIEW]);
    transaction.hide(fragments[EMPTY_VIEW]);
    transaction.commit();

    AsyncTask<String, Void, EventResponseCollection> task = new GetEventsNearByAsyncTask().execute();
    try {
        if(task != null)
            events = task.get().getItems();

    } catch (InterruptedException | ExecutionException e) {
        Log.d("viewEvent", "Error when retrieving task details " + e.getMessage());
    }

    if(events == null)
    {
        //show create your own event fragment
        changeFragment(EMPTY_VIEW);
    }
    else
    {
        Log.d("viewEvent", "I have " + events.size());
        //show proper fragment with the first event displayed
        currentEvent = events.get(eventIndex);
        ((EventDisplayFragment) fragments[EVENT_VIEW]).updateFragment(currentEvent);
        changeFragment(EVENT_VIEW);
    }
}

    @Override
public boolean onOptionsItemSelected(MenuItem item)
{
    Log.d("onOptionsItemSelected", "In the on item select");
    //Pass the event to ActionBarDrawerToggle, if it returns
    //true, then it has handled the app icon touch event
    if(mDrawerToggle.onOptionsItemSelected(item))
    {
        Log.d("onOptionsItemSelected", "Clicked on action bar item");
        return true;
    }

    //Handle your other action bar items...

    return super.onOptionsItemSelected(item);
}

有没有人对我能做什么有任何建议?我已经尝试过我在网上找到的所有解决方案来完成这项工作,但显然我运气不好,似乎没有任何工作:(

5 个答案:

答案 0 :(得分:16)

你必须在你的活动

中添加这样的东西
@Override
public boolean onOptionsItemSelected(android.view.MenuItem item) {
    return drawerToggle.onOptionsItemSelected(item);
}

答案 1 :(得分:8)

这对我有用,它几乎就像上一篇文章

public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
      } }

答案 2 :(得分:2)

好的,这是我身边的一个小遗漏,我忘了包含以下两行代码:

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

在onCreate方法中

答案 3 :(得分:0)

我遇到了类似的问题。 我希望这个答案可以帮助那些面临类似问题的人。 检查抽屉布局中的openDrawer属性

tools:openDrawer="start"

另外,检查活动中的抽屉锁定模式,如果您不小心锁定了抽屉,请将其取下。

//drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

答案 4 :(得分:0)

多年以后但无论如何.....它可以帮助任何人,当你有一个带有Drawerlayout的布局时,抽屉假定第一个孩子是显示时重叠的视图而第二个孩子是导航面板。所以在你的情况下,你可以做一些像......

temp[,.(from=rep(V,(.N-1):0),to=V[unlist(sapply(2:.N, seq, .N, simplify = T))]), by=GR] 

所以这样,Drawer知道RelativeLayout是要重叠的视图,而ListView是你想要的左面板。

此外,你必须在java代码中做所有你知道的事情..设置工具栏,设置抽屉指示工具栏,设置toogle等等......它都在评论中。