我正在使用`implementation'androidx.constraintlayout:constraintlayout:1.1.3'
下面的布局是我的应用程序的根目录。单击BottomNavigation时,会将片段添加到FrameLayout @+id/fragment_container
。每个片段都将其“标题”添加到AppBarLayout
中。在以下情况下,其为CollapsingToolbarLayout。
我使用以下行为https://stackoverflow.com/a/35405095/5925091来偏移底部,但是当直接滚动CollapsingToolbarLayout
时,它会闪烁。从RecyclerView中滚动时,一切正常。
https://gfycat.com/UnequaledSandyAstrangiacoral
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/cl_coordinator"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp" />
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="xyz.FixScrollingFooterBehavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.aurelhubert.ahbottomnavigation.AHBottomNavigation
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.appbar.CollapsingToolbarLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:collapsedTitleTextAppearance="@style/CollapsingToolbarTitleAppearance"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleTextAppearance="@style/CollapsingToolbarTitleAppearance"
app:layout_scrollFlags="scroll|exitUntilCollapsed|enterAlwaysCollapsed">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
// multiple views
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:minHeight="?attr/actionBarSize"
android:theme="@style/Theme.GuidelinesCompat.Toolbar"
app:layout_collapseMode="pin"
app:popupTheme="@style/Theme.GuidelinesCompat.Toolbar.PopUp" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
public class FixScrollingFooterBehavior extends AppBarLayout.ScrollingViewBehavior {
public FixScrollingFooterBehavior() {
super();
}
public FixScrollingFooterBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
final boolean result = super.onDependentViewChanged(parent, child, dependency);
final int bottomPadding = calculateBottomPadding((AppBarLayout) dependency);
final boolean paddingChanged = bottomPadding != child.getPaddingBottom();
if (paddingChanged) {
child.setPadding(
child.getPaddingLeft(),
child.getPaddingTop(),
child.getPaddingRight(),
bottomPadding);
child.requestLayout();
}
return paddingChanged || result;
}
private int calculateBottomPadding(AppBarLayout dependency) {
final int totalScrollRange = dependency.getTotalScrollRange();
return totalScrollRange + dependency.getTop();
}
}