我正在尝试使用自定义单击事件来实现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?根据Google的文档,关键仅在于此:
drawerToggle.setDrawerIndicatorEnabled(false);
。谢谢。 最佳答案
我以一个应用程序为例。请参阅下面。请特别注意MainActivity.java
中的三个注释,这些注释描述了应如何实现侦听器。
图片超过一千个字:
Default behaviour
Custom behaviour
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/