我正在开发一个Android应用,其中操作栏中有抽屉布局。我已经为drawerlayout添加了监听器,以便在抽屉打开或关闭时进行监听。抽屉布局附有导航视图。
现在可以通过各种方式打开和关闭抽屉:点击打开,滑动打开,滑动到关闭以及点击外部关闭。我必须添加事件来分别识别这些事件。
我已经以这种方式添加了drawerlayout和navigationview:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
>
<include
android:id="@+id/toolbar"
layout="@layout/tool_bar"
/>
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
app:headerLayout="@layout/header"
app:menu="@menu/drawer"
/>
Noe在Activity中,我在navigationview和drawerlayout上添加了以下监听器。
//Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
//Checking if the item is in checked state or not, if not make it in checked state
if(menuItem.isChecked()) menuItem.setChecked(false);
else menuItem.setChecked(true);
//Closing drawer on item click
drawerLayout.closeDrawers();
//Check to see which item was being clicked and perform appropriate action
switch (menuItem.getItemId()){
// Switch case identify different options
}
}
});
// Initializing Drawer Layout and ActionBarToggle
drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.openDrawer, R.string.closeDrawer){
@Override
public void onDrawerClosed(View drawerView) {
// Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerOpened(View drawerView) {
// Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank
super.onDrawerOpened(drawerView);
}
};
//Setting the actionbarToggle to drawer layout
drawerLayout.setDrawerListener(actionBarDrawerToggle);
//calling sync state is necessay or else your hamburger icon wont show up
actionBarDrawerToggle.syncState();
现在onDrawerOpened
和onDrawerClosed
仅向我提供有关何时打开和关闭抽屉的信息。如何区分开放和关闭的交互?用于识别的交互:tap-to-open, swipe-to-open, swipe-to-close and tap-outside-to-close
。
答案 0 :(得分:0)
ActionBarDrawerToggle
提供的不仅仅是onDrawerClosed
,onDrawerOpened
方法。您覆盖onDrawerSlide
方法以检测幻灯片:
..
@Override
public void onDrawerSlide (View drawerView,float slideOffset){
if(determine if openning or closing){
doStuff();
}else{
doOtherStuff();
}
}
可以通过覆盖Activity.onOptionsItemSelected(MenuItem item)
方法来检测工具栏上的点按。然后检查该项是否具有ID android.R.id.home
。
在此处阅读更多内容:https://developer.android.com/reference/android/support/v7/app/ActionBarDrawerToggle.html
答案 1 :(得分:0)
我找到了一种方法,可以通过滑动,使用一些现有参数和编写不同的方法来准确确定抽屉是否已打开:
@Override
public void onDrawerStateChanged(int newState) {
if (newState == DrawerLayout.STATE_DRAGGING) {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
// drawer is currently open and is being closed by sliding
} else {
// drawer is currently closed and is being opened by sliding
}
}
}
一旦我知道,我会添加正确检测点击事件。如果有人知道,请发表评论。