1

我正在尝试使用自定义单击事件来实现 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);. 谢谢你。

4

3 回答 3

3

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

图片一千多字:

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>
于 2015-12-31T11:12:18.867 回答
3

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

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

于 2016-07-19T15:02:57.263 回答
1

这可能为时已晚,但我努力寻找一种方法来捕获导航抽屉图标的 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);
            }
        });
于 2019-01-02T15:23:55.190 回答