gpt4 book ai didi

java - float 操作按钮未显示在 bottomsheetfragment 折叠状态

转载 作者:行者123 更新时间:2023-12-01 11:19:37 27 4
gpt4 key购买 nike

我有 BottomSheetFragmentrecyclerviewfab按钮。我在显示 Floating action button 时遇到问题在 BottomSheetBehavior.STATE_COLLAPSED .一旦我将 Bottom Sheet 扩展到全屏,就会出现 Fab 按钮,我尝试了几种方法,但都没有奏效。

我在布局中尝试了不同的 fab 选项以使其可见,但到目前为止还没有运气。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/topBarBottomSheet"
android:clipToPadding="true"
android:layout_height="match_parent"
android:layout_width="match_parent"
>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/topBarBottomSheet">
<include layout="@layout/progressbar_framelayout" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycleviewGallery"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingTop="@dimen/list_item_spacing_half"
android:paddingBottom="@dimen/list_item_spacing_half"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:listitem="@layout/recycler_view_item_3"
tools:spanCount="3"
tools:layoutManager="GridLayoutManager" />



</FrameLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fabBottomSheet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
app:backgroundTint="@color/greenElaxer"
app:fabSize="normal"
app:srcCompat="@drawable/ic_check"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"

android:overScrollMode="always"

>

</com.google.android.material.floatingactionbutton.FloatingActionButton>


</RelativeLayout>

BottomSheetFragment
public class BottomSheet extends BottomSheetDialogFragment {

FloatingActionButton fab;
RecyclerView recyclerView;
// TODO: Customize parameters
public static BottomSheet newInstance() {
/*final Bundle args = new Bundle();
args.putInt(ARG_ITEM_COUNT, itemCount);
fragment.setArguments(args);*/

return new BottomSheet();
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {

return inflater.inflate(R.layout.fragment_bottemsheet_list_dialog, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
recyclerView = view.findViewById(R.id.recycleviewGallery);
fab = view.findViewById(R.id.fabBottomSheet);
BottomSheetAdapter bottomSheetAdapter = new BottomSheetAdapter();
GridLayoutManager gridLayoutManager=new GridLayoutManager(getActivity(),2);
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(bottomSheetAdapter);
}


@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
//Get the BottomSheetBehavior
BottomSheetDialog d = (BottomSheetDialog) dialog;
FrameLayout bottomSheet = d.findViewById(com.google.android.material.R.id.design_bottom_sheet);
if (bottomSheet != null) {
bottomSheet.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
bottomSheet.setMinimumHeight(350);
bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View view, int i) {
switch (i){
case BottomSheetBehavior.STATE_COLLAPSED:
Log.d(TAG,"Collapsed");

break;
case BottomSheetBehavior.STATE_DRAGGING:
Log.d(TAG,"Dragging");

break;
case BottomSheetBehavior.STATE_EXPANDED:
Log.d(TAG,"Expanded");

break;
case BottomSheetBehavior.STATE_HALF_EXPANDED:
Log.d(TAG,"Half Expanded");
break;
case BottomSheetBehavior.STATE_HIDDEN:
Log.d(TAG,"Hidden");
dismiss();
break;
case BottomSheetBehavior.STATE_SETTLING:
Log.d(TAG,"Settling");
break;

}
}

@Override
public void onSlide(@NonNull View view, float v) {

}
});
}
}
});

return dialog;
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
final Fragment parent = getParentFragment();
Log.d(TAG,"Parent = "+parent+" Context "+context);
if (parent != null) {
mListener = (Listener) parent;
} else {
mListener = (Listener) context;
}
}

@Override
public void onDetach() {
mListener = null;
super.onDetach();
}


}

每当我折叠 Bottom Sheet 时,fab 按钮也会与 Bottom Sheet 一起下降。无论我展开还是折叠 Bottom Sheet ,我都需要让我的 fab 按钮粘在同一个地方。提前致谢。

我需要坚持相同的布局(相对布局)

最佳答案

当您退出该 fragment 时,您无法使某个 fragment 中的 UI 元素保持不变。如果 Bottom Sheet 折叠,则其所有 UI 元素都将折叠。你不能坚持下去。

您可以编写一个重复的 fab,在您关闭底层 fragment 中的 Bottom Sheet 后显示。

例如,我在 Bottom Sheet 中有这段代码来退出 Bottom Sheet 对话框。

nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mBottomSheetDialog.dismiss();
mTimePickerDialogController.show(0, 0, makeTimePickerDialogTag());
}});

当您关闭 Bottom Sheet 时,您可以使用以下代码使基础 fragment 中的晶圆厂出现或消失。
mFloatingActionButton.hide();
mFloatingActionButton.show();

编辑:处理第二个晶圆厂的更好解决方案。
BottomSheetBehavior sheetBehavior;
sheetBehavior = BottomSheetBehavior.from(yourBottomSheet);

sheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
switch (newState) {
case BottomSheetBehavior.STATE_HIDDEN:
mFloatingActionButton.show();
case BottomSheetBehavior.STATE_EXPANDED: {
mFloatingActionButton.hide();
}
break;
case BottomSheetBehavior.STATE_COLLAPSED: {
mFloatingActionButton.show();
}
}
}

关于java - float 操作按钮未显示在 bottomsheetfragment 折叠状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59691214/

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