浮动操作按钮未显示在 bottomsheetfragment 折叠状态
Posted
技术标签:
【中文标题】浮动操作按钮未显示在 bottomsheetfragment 折叠状态【英文标题】:Floating action button not showing in bottomsheetfragment collapsed state 【发布时间】:2020-04-28 15:59:59 【问题描述】:我有 BottomSheetFragment
和 recyclerview
和 fab
按钮。我在BottomSheetBehavior.STATE_COLLAPSED
中显示Floating action button
时遇到问题。一旦我将底部工作表扩展到全屏,就会出现 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_
android:layout_
>
<FrameLayout
android:layout_
android:layout_
android:layout_below="@id/topBarBottomSheet">
<include layout="@layout/progressbar_framelayout" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycleviewGallery"
android:layout_
android:layout_
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_
android:layout_
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();
每当我折叠底部工作表时,fab 按钮也会与底部工作表一起下降。无论我展开还是折叠底部工作表,我都需要让我的 fab 按钮粘在同一位置。提前致谢。
我需要坚持相同的布局(相对布局)
【问题讨论】:
为什么我在你的布局中看不到BottomSheet
?它的 id 可能是 topBarBottomSheet
但它不存在!
@Bahman 因为我正在使用BottomSheetFragment
但是你有这个代码:android:layout_below="@id/topBarBottomSheet"
,而你没有android:id="@+id/topBarBottomSheet"
。 layout_below
必须引用当前 RelativeLayout
根中的视图 ID。
@Bahman 如果你再看一遍,它总是在那里
我又看了一遍,但你对android:id="@+id/topBarBottomSheet"
没有任何看法。我使用“Ctrl + F”在您的代码中查找“topBarBottomSheet”,但您使用了此 id 而没有声明它。
【参考方案1】:
当您退出某个 Fragment 时,您无法让某个 Fragment 中的 UI 元素保持不变。如果底页折叠,则其所有 UI 元素都将折叠。你不能坚持下去。
您可以编写一个重复的 fab,在您关闭底层片段中的底页后显示。
例如,我在底页中有这段代码来退出底页对话框。
nextButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
mBottomSheetDialog.dismiss();
mTimePickerDialogController.show(0, 0, makeTimePickerDialogTag());
);
当您关闭底页时,您可以使用以下代码使底层片段中的 fab 出现或消失。
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();
【讨论】:
以上是关于浮动操作按钮未显示在 bottomsheetfragment 折叠状态的主要内容,如果未能解决你的问题,请参考以下文章