gpt4 book ai didi

android - 如何使底部工作表在展开时将工具栏推出并在折叠时将其拉回?

转载 作者:太空宇宙 更新时间:2023-11-03 13:11:10 24 4
gpt4 key购买 nike

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

    <?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 - 如何使底部工作表在展开时将工具栏推出并在折叠时将其拉回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44674333/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com