1

我希望当底部工作表展开时向上推动工具栏,并在底部工作表展开时拉回工具栏。我看到的所有示例都是其他视图,它们合并到工具栏中,或者捕捉到工具栏顶部,但没有一个隐藏工具栏。如何做到这一点?如何在展开时将底部工作表推出工具栏,并在折叠时将其拉回?

<?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> 
+0

你可以使用折叠工具栏为此目的 –

+0

@NileshRathod你可以更具体一点或指向我的例子吗? – Nima

回答

0

不喜欢在以下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 = “@串/ appbar_scrolling_view_behavior” 内部android.support.v4.widget.NestedScrollView像的下方。

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; 
    } 
} 

它只需按下工具栏并改变可视性:

<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> 
+0

这不是我想要做的,底部有一个底部,顶部有一个工具栏。当底部工作表展开并在折叠时拉下/返回到屏幕时,工具栏需要被推上/拉出屏幕。 – Nima

+0

@Nima检查我的更新答案 –

1

只是新的行为类:

<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> 

满级。如果你想要一些更具体的行为,只需重写onDependentViewChanged方法。

0

如果要将底部工作表显示在工具栏上方,可以将android:elevation = 4dp添加到底部工作表。这将确保当您展开底部工作表时,它将覆盖工具栏。通过这种方式,您可以避免隐藏/显示工具栏的麻烦。

相关问题