gpt4 book ai didi

android - 如何让多个底部工作表正常工作?

转载 作者:行者123 更新时间:2023-12-04 15:46:18 25 4
gpt4 key购买 nike

我有两个底部工作表,您可以从底部拖动它们。您首先拖动一个底部工作表,然后您可以在其上拖动另一个。但是,如果您解雇一个,另一个也会被解雇。我希望能够先关闭顶层,然后逐个关闭底部。我试过禁用底部的,但都被禁用了。

最佳答案

好的,我解决了这个问题。您需要为此创建自定义底部工作表行为。

有两个底页的布局:

<FrameLayout
android:id="@+id/first_bottom_sheet_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="com.xxx.com.CustomBottomSheetBehavior"
app:behavior_hideable="false"
app:behavior_peekHeight="@{model.first_height}">
<FrameLayout
android:id="@+id/first_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</FrameLayout>

<FrameLayout
android:id="@+id/second_bottom_sheet_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="com.xxx.com.CustomBottomSheetBehavior"
app:behavior_hideable="false"
app:behavior_peekHeight="@{model.second_height}">
<FrameLayout
android:id="@+id/second_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</FrameLayout>

我把我要展开的两个fragment从底部添加到上面布局的fragment container中。

getSupportFragmentManager().beginTransaction()
.add(R.id.first_fragment_container, new FirstFragment())
.commit();

getSupportFragmentManager().beginTransaction()
.add(R.id.second_fragment_container, new SecondFragment())
.commit();

然后我创建了一个自定义底部工作表行为。

public class CustomBottomSheetBehavior<V extends View> extends BottomSheetBehavior<V> 
{


private boolean enableCollapse = true;

public CustomBottomSheetBehavior() {
}

public CustomBottomSheetBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}

public void setEnableCollapse(boolean enableCollapse) {
this.enableCollapse = enableCollapse;
}

@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
if (enableCollapse==true) {
return false;
}

return super.onInterceptTouchEvent(parent, child, event);

}
}

现在您将回调添加到上面的底部工作表。

FrameLayout firstBottomSheet = activity.findViewById(R.id.first_bottom_sheet_holder);
FrameLayout secondBottomSheet = activity.findViewById(R.id.second_bottom_sheet_holder);
CustomBottomSheetBehavior firstBottomSheetBehavior = (CustomBottomSheetBehavior)BottomSheetBehavior.from(firstBottomSheet);
CustomBottomSheetBehavior secondBottomSheetBehaviour = (CustomBottomSheetBehavior)BottomSheetBehavior.from(secondBottomSheet);


firstBottomSheetBehaviour.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {



}

@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {

}
});





secondBottomSheetBehaviour.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {


if (newState == BottomSheetBehavior.STATE_DRAGGING||newState==BottomSheetBehavior.STATE_EXPANDED) {

firstBottomSheetBehavior .setEnableCollapse(false);

}else if(newState == BottomSheetBehavior.STATE_COLLAPSED) {
firstBottomSheetBehavior .setEnableCollapse(true);

}
}

@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {

}
});

全部完成。

关于android - 如何让多个底部工作表正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55639044/

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