我们可以通过对AppBarLayout进行子类化并重写其onStartNestedScroll方法,来解决nestedScrollingEnabled无效的问题。具体实现如下:
public class FixedAppBarLayout extends AppBarLayout { private boolean mIsEnabled;
public FixedAppBarLayout(Context context) {
super(context);
mIsEnabled = true;
}
public FixedAppBarLayout(Context context, AttributeSet attrs) {
super(context, attrs);
mIsEnabled = true;
}
@Override
public void setNestedScrollingEnabled(boolean enabled) {
super.setNestedScrollingEnabled(enabled);
mIsEnabled = enabled;
}
@Override
public boolean isNestedScrollingEnabled() {
return mIsEnabled;
}
@Override
public boolean onStartNestedScroll(CoordinatorLayout parent, View child, View directTargetChild, View target, int nestedScrollAxes) {
// Check if nested scrolling is enabled first
if (mIsEnabled) {
return super.onStartNestedScroll(parent, child, directTargetChild, target, nestedScrollAxes);
}
return false;
}
}
在xml文件中使用FixedAppBarLayout代替原来的AppBarLayout即可解决问题: