我正在尝试使用自定义点击事件实现 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);
结果:永远不会触发点击事件。
如何将自定义点击事件附加到 ActionBarDrawerToggle?根据谷歌的文档,关键只有这个:drawerToggle.setDrawerIndicatorEnabled(false);
。谢谢。
最佳答案
我创建了一个应用程序作为示例。请看下面。请特别注意 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>
关于android - ActionBarDrawerToggle 的 setToolbarNavigationClickListener 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33636104/