【问题标题】:ActionBarDrawerToggle's setToolbarNavigationClickListener doesn't workActionBarDrawerToggle 的 setToolbarNavigationClickListener 不起作用
【发布时间】:2015-11-10 17:35:27
【问题描述】:

我正在尝试使用自定义点击事件来实现 ActionBarDrawerToggle,但它似乎不起作用。

我已经像这样创建了新的 ActionBarDrawerToggle:

drawerToggle = new ActionBarDrawerToggle(activity, drawer, R.string.open, R.string.close);

像这样设置 ActionBar:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);

像这样添加了自定义监听器:

drawerToggle.setDrawerIndicatorEnabled(false);
drawerToggle.setToolbarNavigationClickListener(myListener);

结果:从不触发 Click 事件。

如何将自定义点击事件附加到 ActionBarDrawerToggle?根据谷歌的文档,关键只有这个:drawerToggle.setDrawerIndicatorEnabled(false);。谢谢。

【问题讨论】:

    标签: android actionbardrawertoggle


    【解决方案1】:

    我创建了一个应用程序作为示例。请看下文。请特别注意 MainActivity.java 中的三个 cmets,它们描述了您应该如何实现侦听器。

    图片一千多字:

    MainActivity.java

    import android.content.res.Configuration;
    import android.os.Bundle;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.ActionBarDrawerToggle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.MenuItem;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
    
        private ActionBarDrawerToggle mDrawerToggle;
        private DrawerLayout mDrawerLayout;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    
            mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
            // 1.
            // if you want the default behaviour for ActionBarDrawerToggle
            // uncomment the first line below and comment out the second one
    //        mDrawerLayout.setDrawerListener(mDrawerToggle);
            mDrawerToggle.setDrawerIndicatorEnabled(false);
        }
    
        @Override
        protected void onPostCreate(Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
            mDrawerToggle.syncState();
        }
    
        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            mDrawerToggle.onConfigurationChanged(newConfig);
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // 2.
            // the default behaviour for ActionBarDrawerToggle
            if (mDrawerToggle.onOptionsItemSelected(item)) {
                return true;
            }
    
            int id = item.getItemId();
    
            if (id == android.R.id.home) {
                // 3.
                // here you can define your custom click listener / onClick method
                // for ActionBarDrawerToggle
                String customClick = getResources().getString(R.string.custom_click_event);
                Toast.makeText(this, customClick, Toast.LENGTH_LONG).show();
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    }
    

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context=".MainActivity">
    
        <!-- content -->
        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="@string/content"/>
    
        </FrameLayout>
    
        <!-- navigation drawer -->
        <FrameLayout
            android:id="@+id/navigation_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            android:background="#00ff00">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="@string/navigation_item"/>
    
        </FrameLayout>
    
    </android.support.v4.widget.DrawerLayout>
    

    【讨论】:

      【解决方案2】:

      它不起作用,因为您使用了不接受工具栏参数的ActionBarDrawerToggle 重载。只有当您创建这样的切换时,才会正确调用此侦听器:

      drawerToggle = new ActionBarDrawerToggle(活动, 抽屉, 工具栏, R.string.open, R.string.close);

      【讨论】:

        【解决方案3】:

        这可能已经晚了,但我努力寻找一种方法来捕获导航抽屉图标的 onClick 事件。这是我发现的:

                Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
                toolbar.setTitle("Asset Locator");
                setSupportActionBar(toolbar);
        
                drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
                toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
                drawer.addDrawerListener(toggle);
                toggle.syncState();
        
                toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
        
                      // You have to manually toggle drawer and set icon here
                      if (drawer.isDrawerOpen(GravityCompat.START))
                          drawer.closeDrawer(GravityCompat.START);
                      else
                          drawer.openDrawer((int) Gravity.START);
                    }
                });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多