我希望工具栏在底部工作表展开时向上推出视图,并在折叠时拉回视图。我看到的所有示例都是其他视图合并到工具栏中或与工具栏对齐到顶部,但它们都没有隐藏工具栏。怎么做呢?
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
>
<FrameLayout
android:id="@+id/mainContentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/red"
/>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
>
<FrameLayout
android:id="@+id/topContentContainer"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/blue"
app:layout_scrollFlags="scroll|enterAlways|snap"
/>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/categoriesSelectionContainer"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@color/green"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="android.support.design.widget.AppBarLayout.ScrollingViewBehavior"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
/>
</android.support.v4.widget.NestedScrollView>
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
只是新的行为类:
<android.support.design.widget.CoordinatorLayout
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">
<!--it can be any view-->
<android.support.v7.widget.Toolbar
android:id="@+id/appbarlayout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_behavior="/*full path to class*/HidingViewWithBottomSheetBehavior">
</android.support.v7.widget.Toolbar>
<!--it can be any view-->
<FrameLayout
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:behavior_peekHeight="50dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
</FrameLayout>
全班:
public class HidingViewWithBottomSheetBehavior extends AppBarLayout.ScrollingViewBehavior {
private static final float UNDEFINED = Float.MAX_VALUE;
private float childStartY = UNDEFINED;
public HidingViewWithBottomSheetBehavior() {
}
public HidingViewWithBottomSheetBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
return getBottomSheetBehavior(dependency) != null;
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
BottomSheetBehavior bottomSheetBehavior = getBottomSheetBehavior(dependency);
if (bottomSheetBehavior != null) {
float slideOffset = getSlideOffset(parent, dependency, bottomSheetBehavior);
child.setAlpha(1 - slideOffset);
if (childStartY == UNDEFINED) {
childStartY = child.getY();
}
int childHeight = child.getMeasuredHeight();
float childY = childStartY - (childHeight * slideOffset);
child.setY(childY);
}
return true;
}
private float getSlideOffset(CoordinatorLayout parent, View dependency, BottomSheetBehavior bottomSheetBehavior) {
int parentHeight = parent.getMeasuredHeight();
float sheetY = dependency.getY();
int peekHeight = bottomSheetBehavior.getPeekHeight();
int sheetHeight = dependency.getHeight();
float collapseY = parentHeight - peekHeight;
float expandY = parentHeight - sheetHeight;
float deltaY = collapseY - expandY;
return (parentHeight - peekHeight - sheetY) / deltaY;
}
@Nullable
private BottomSheetBehavior getBottomSheetBehavior(@NonNull View view) {
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) view.getLayoutParams();
CoordinatorLayout.Behavior behavior = params.getBehavior();
if (behavior instanceof BottomSheetBehavior) {
return (BottomSheetBehavior) behavior;
}
return null;
}
}
它只是将工具栏推出并更改可见性。如果你想要一些更具体的行为,只需重写 onDependentViewChanged 方法。
如果您希望底部工作表显示在工具栏上方,则可以将android:elevation = 4dp
添加到底部工作表。这将确保当您展开底部工作表时,它会覆盖工具栏。通过这种方式,您可以避免隐藏/显示工具栏的麻烦。
activity_main.XML的CoordinatorLayout中做一些更改,如下所示(根据您的要求更改主题(。
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
比 :layout_behavior="@string/appbar_scrolling_view_behavior" 在 android.support.v4.widget.NestedScrollView 中如下所示。
<android.support.v4.widget.NestedScrollView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello "
android:textSize="25dp"
android:textStyle="bold" />
/// Add your other code here
</LinearLayout>
</android.support.v4.widget.NestedScrollView>