Android:一旦我以编程方式禁用滚动视图行为,我将无法再次启用它
Posted
技术标签:
【中文标题】Android:一旦我以编程方式禁用滚动视图行为,我将无法再次启用它【英文标题】:Android: Once I disable scrollingviewbehavior programmatically I can't enable it again 【发布时间】:2017-12-17 20:33:53 【问题描述】:根据我当时打开抽屉布局的要求,我想禁用滚动行为,当我关闭抽屉布局时,我想启用它。我可以在打开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_
android:layout_>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/myCoordinator"
android:layout_
android:layout_>
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_
android:layout_
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<include layout="@layout/toolbar" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/frameContainer"
android:layout_
android:layout_
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.DrawerLayout>
【问题讨论】:
【参考方案1】:对于锁定,您可以这样做:
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
解锁:
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
【讨论】:
我不是在谈论抽屉布局。禁用 CoordinatorLayout 后,我无法以编程方式启用它的滚动行为。以上是关于Android:一旦我以编程方式禁用滚动视图行为,我将无法再次启用它的主要内容,如果未能解决你的问题,请参考以下文章