根据我的要求,当我打开drawerLayout时,我想禁用滚动行为,当我关闭drawerLayout然后我想启用它。我可以在打开drawerLayout时禁用滚动行为,但在关闭adrawerLayout时无法启用它。
以下是我的代码
DrawerLayout mdrawer_layout = (DrawerLayout) getParentFragment().getView().findViewById(R.id.drawer_layout);
mdrawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
mdrawer_layout.addDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
}
@Override
public void onDrawerOpened(View drawerView) {
Toolbar toolbar = (Toolbar) mActivity.findViewById(R.id.actionBar);
AppBarLayout appbar = (AppBarLayout) mActivity.findViewById(R.id.appBarLayout);
/*AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
params.setScrollFlags(0);*/
CoordinatorLayout.LayoutParams appBarLayoutParams = (CoordinatorLayout.LayoutParams) appbar.getLayoutParams();
appBarLayoutParams.setBehavior(null);
appbar.setLayoutParams(appBarLayoutParams);
}
@Override
public void onDrawerClosed(View drawerView) {
Toolbar toolbar = (Toolbar) mActivity.findViewById(R.id.actionBar);
AppBarLayout appbar = (AppBarLayout) mActivity.findViewById(R.id.appBarLayout);
FrameLayout frameContainer = (FrameLayout) mActivity.findViewById(R.id.frameContainer);
/*AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
params.setScrollFlags(params.SCROLL_FLAG_ENTER_ALWAYS);
toolbar.setLayoutParams(params);*/
CoordinatorLayout.LayoutParams appBarLayoutParams = (CoordinatorLayout.LayoutParams) frameContainer.getLayoutParams();
appBarLayoutParams.setBehavior(new AppBarLayout.ScrollingViewBehavior());
frameContainer.setLayoutParams(appBarLayoutParams);
appBarLayoutParams = (CoordinatorLayout.LayoutParams) appbar.getLayoutParams();
appBarLayoutParams.setBehavior(new AppBarLayout.ScrollingViewBehavior());
appbar.setLayoutParams(appBarLayoutParams);
}
@Override
public void onDrawerStateChanged(int newState) {
}
});
我的布局文件如下。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/myCoordinator"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<include layout="@layout/toolbar" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/frameContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.DrawerLayout>
答案 0 :(得分:0)
锁定你可以这样做:
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
和解锁:
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);