【发布时间】: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_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();
}
}
每当我折叠底部工作表时,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 而没有声明它。
标签: java android floating-action-button bottom-sheet