您好,我有一个底部工作表对话框片段,当单击回收器视图中的项目时会显示该片段。底部表单实现的显示位于回收视图的适配器中。我遇到的问题是,当您快速双击项目以显示底部工作表时,它会显示两次,是否有一种方法可以将点击限制为仅一次点击,或者在显示底部工作表对话框片段时不再显示底部工作表对话框片段通过检查
以下是如何在回收视图中显示项目单击的底部工作表
@Override
public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, final int position) {
myViewHolder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BottomSheetFragment bottomSheetFragment = new BottomSheetFragment();
Bundle bundle = new Bundle();
bundle.putInt("id",productList.get(position).getId());
bundle.putString("name",productList.get(position).getName());
bundle.putDouble("price",productList.get(position).getPrice());
bundle.putInt("stock",productList.get(position).getStock());
bundle.putInt("quantity",productList.get(position).getQuantity());
bundle.putString("photo",productList.get(position).getPhoto());
bottomSheetFragment.setArguments(bundle);
bottomSheetFragment.show(fragmentManager, bottomSheetFragment.getTag());
}
});
}
Run Code Online (Sandbox Code Playgroud)
我尝试通过执行以下操作来使用 Muhannad Fakhouri 答案
声明一个布尔值来显示 BottomSheet 是否显示
private boolean isBottomSheetShowing = false;
Run Code Online (Sandbox Code Playgroud)
底部工作表中的实现
if(!isBottomSheetShowing){
isBottomSheetShowing = true;
ItemBottomSheet itemBottomSheet = new ItemBottomSheet();
Bundle bundle = new Bundle();
bundle.putString("code",itemPosition.getCode());
bundle.putString("name",itemPosition.getName());
bundle.putString("description",itemPosition.getDescription());
bundle.putString("upcCode",itemPosition.getUpcCode());
bundle.putString("photoBlob",itemPosition.getPhotoBlob());
bundle.putDouble("discount",itemPosition.getDiscount());
bundle.putDouble("price",itemPosition.getPrice());
bundle.putInt("available",itemPosition.getAvailable());
itemBottomSheet.setArguments(bundle); …Run Code Online (Sandbox Code Playgroud) android android-studio bottom-sheet android-bottomsheetdialog
我正在使用https://pub.dev/packages/persistent_bottom_nav_bar包来显示底部栏。当按下底部栏选项卡之一时,我需要打开底部表格。我已设法使用 showModalBottomSheet 显示底部工作表,但问题是它覆盖了现有的底部栏。我需要打开底部栏上方的底部工作表,而不是从屏幕底部打开。这就是我想要实现的目标。
所以我设法使用带有容器和底部边距的 showGeneralDialog 进行显示。以下是我的解决方案。
showGeneralDialog(
barrierLabel: AppLocalizations.of(context).translate(LanguageConstant.more),
barrierDismissible: true,
barrierColor: Colors.grey.withOpacity(0.1),
transitionDuration: Duration(milliseconds: 100),
context: context,
pageBuilder: (dialogContext, anim1, anim2) {
return Align(
alignment: Alignment.bottomCenter,
child: Container(
margin: EdgeInsets.only(bottom: CommonConstants.BOTTOM_BAR_HEIGHT, top: MediaQuery.of(context).size.height/2, left: 0, right: 0),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surface,
border: Border.all(width: 0.5,color: ColorConstants.blackShade3),
),
child: <Your child widgets>,
),
);
},
transitionBuilder: (dialogContext, anim1, anim2, child) {
return SlideTransition(
position: Tween(begin: Offset(0, 1), end: Offset(0, 0)).animate(anim1),
child: child,
);
},
);
Run Code Online (Sandbox Code Playgroud) 我遇到一种情况,我希望在点击小部件时可以看到模态底部工作表。此代码可以正常工作(来自小部件,它基本上是一张“卡片”):
return Container(
color: Colors.white,
margin: EdgeInsets.symmetric(horizontal: 5.0),
child: Material(
child: InkWell(
onTap: () {
showMaterialModalBottomSheet(
expand: false,
context: context,
builder: (context) =>
customiseItemScreen(item: this.item,),
);
},
...
...
Run Code Online (Sandbox Code Playgroud)
但是,我还想在customiseItemScreen小部件中显示一个浮动操作按钮。当涉及到脚手架时,很容易理解:
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
floatingActionButton:
...
...
Run Code Online (Sandbox Code Playgroud)
但由于customiseItemScreen返回一个材质(没有支架),因此上述成员不存在。有没有更好的方法来解决这个问题,或者现有代码中可能缺少的解决方案?
提前致谢,
我试图找到一种方法来从应用程序中的任何位置显示 BottomSheet,因为我使用 和BottomSheetScaffold一个 LiveData 对象来保存当前可组合函数,该函数被观察为状态:
val sheetContent by MusicHub.state.bottomSheet.getContent().observeAsState()
BottomSheetScaffold(
modifier = Modifier
.fillMaxSize(),
scaffoldState = bottomSheetScaffoldState,
sheetPeekHeight = 0.dp,
sheetContent = sheetContent!!
)
Run Code Online (Sandbox Code Playgroud)
我在我的应用程序中使用 BottomSheet 作为上下文菜单。例如,当我长按应用程序中的播放列表时,它会设置 BottomSheet 的内容并显示如下:
PlaylistItem(
// ...
onLongClick = {
// Set the LiveData composable
MusicHub.state.bottomSheet.setContent {
PlaylistContextMenuTest(playlist!!, viewModel)
}
// Expand BottomSheet
scope.launch {
MusicHub.state.bottomSheet.expand()
}
}
)
Run Code Online (Sandbox Code Playgroud)
一般来说,这是可行的,但第一次展开 BottomSheet 时,它会显示一瞬间,然后再次消失在底部。这是一个小 GIF:
我的猜测是,不知何故,BottomSheet 的大小尚未重新计算,因此它仅在下一次重组中起作用。来自网络开发人员,我会说这是 requestAnimationFrame 的典型案例,但我不太知道如何在撰写中解决这个问题。
编辑:
PlaylistContextMenu测试代码:
@Composable
fun PlaylistContextMenuTest(playlist: Playlist, viewModel: LibraryViewModel = activityViewModel()){
val scrollState = rememberScrollState()
Column(
modifier …Run Code Online (Sandbox Code Playgroud) 我正在开发一个 flutter 应用程序,该应用程序有一个页面,其中包含一个(圆形凹口矩形)BottomAppBar、一个 FloatingActionButton 和一个 BottomSheet,当用户单击地图上的标记时会出现该页面。我的问题是,当 BottomSheet 显示在屏幕上时,它将 FloatingActionButton 向上移动到 BottomSheet 的顶部。以下是我的应用程序中发生的情况的屏幕截图:
即使打开 BottomSheet,我也想将此 FloatingActionButton 保持在同一位置(因此位于 BottomAppBar 的中间)。是否可以通过更改某些现有属性来做到这一点?或者我是否必须构建自己的 BottomSheet 元素,以便它不会以这种方式与 FloatingActionButton 交互?
这是 Home.dart 中 FloatingActionButton/Scaffold 的代码片段:
@override
Widget build(BuildContext context) {
return Scaffold(
extendBodyBehindAppBar: true,
extendBody: true,
bottomNavigationBar: bottomAppBar(),
floatingActionButton: Container(
height: MediaQuery.of(context).size.width * 0.1625,
width: MediaQuery.of(context).size.width * 0.1625,
child: FittedBox(
child: FloatingActionButton(
elevation: 0.0,
onPressed: () {},
child: Icon(
Icons.bolt,
size: MediaQuery.of(context).size.width * 0.075,
),
),
),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
body: someWidget,
);
} …Run Code Online (Sandbox Code Playgroud) floating-action-button flutter bottom-sheet bottomnavigationview flutter-layout
Bottom_sheet_image_dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:behavior_hideable="false"
app:behavior_peekHeight="62dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/llAddDriverPic"
android:background="?android:attr/selectableItemBackground"
android:padding="8dp">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="@drawable/baseline_add_photo_alternate_24"
android:layout_margin="8dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/add_picture"
android:layout_gravity="center_vertical"
android:padding="8dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/llRemoveDriverPic"
android:background="?android:attr/selectableItemBackground"
android:padding="8dp">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="@drawable/baseline_no_photography_24"
android:layout_margin="8dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/remove_picture"
android:layout_gravity="center_vertical"
android:padding="8dp"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
代码实现:
BottomSheetDialog bsDialog = new BottomSheetDialog(getContext());
bsDialog.setContentView(R.layout.bottom_sheet_image_dialog);
bsDialog.setOnShowListener(dialog -> {
BottomSheetBehavior bottomSheetBehavior = ((BottomSheetDialog)dialog).getBehavior();
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
});
Run Code Online (Sandbox Code Playgroud)
出现以下问题:视频中看到的宽度不会完全显示。如果我在横向模式下从一开始就启动底页,则宽度看起来也会有所不同,因为左侧和右侧未被覆盖:
可能存在什么问题?在显示对话框之前是否需要预先定义宽度?
我用以下 xml 代码创建了一个 BottomSheet:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#C57C3C"
tools:context=".MainActivity">
<TextView
android:id="@+id/tvState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="initial state" />
<LinearLayout
android:id="@+id/linBottomSheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<include
layout="@layout/dialog_bottomsheet_half"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
在我的底部工作表用户界面上,我有三个线性布局。命名为linHeader,linOne和linTwo. 在第一种状态下,这个线性布局消失了,除了linHeader。
我使用此代码来显示我的底页:
val behavior = BottomSheetBehavior.from(linBottomSheet)
behavior.isFitToContents = false
behavior.setBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
override fun onStateChanged(bottomSheet: View, newState: Int) {
when (newState) {
BottomSheetBehavior.STATE_COLLAPSED -> {
Log.e("bottom", "STATE_COLLAPSED")
}
BottomSheetBehavior.STATE_HALF_EXPANDED -> {
Log.e("bottom", "STATE_HALF_EXPANDED")
} …Run Code Online (Sandbox Code Playgroud) 我将 Jetpack Compose 与 BottomSheetScaffold 结合使用。为了能够在可组合项内部和外部显示和隐藏底部工作表,我使用了一个showBottomSheet: MutableState<Boolean>变量。然后可组合项内的窥视高度确定如下:
val baseBottomSheetPeekHeight by remember { mutableStateOf(60.dp) }
val bottomSheetPeekHeight = if (showBottomSheet.value) baseBottomSheetPeekHeight else 0.dp
Run Code Online (Sandbox Code Playgroud)
后来,在 中BottomSheetScaffold,我使用这样的变量:
BottomSheetScaffold(
...
sheetPeekHeight = bottomSheetPeekHeight,
...
)
Run Code Online (Sandbox Code Playgroud)
(完整的重现项目在这里:https://github.com/dbrgn/compose-repro)
这通常按预期工作,我可以设置showBottomSheet.value为false隐藏底部工作表。然而,隐藏看起来很糟糕,因为并非所有子可组合项都同时隐藏。
由于 GIF 转换,在上面的动画中有点难以看到,但是当关闭底部工作表查看窗格时,在底部工作表消失之前,其他内容(位于其下方)会短暂可见。
有没有办法避免这种笨拙的隐藏行为?或者更好的是,有没有一种方法可以平滑地设置窗格隐藏的动画?
我有一个底部表单对话框。对于该对话框,我使用的是导航库ModalBottomSheetDialog中的Accompanist。在名为 的可组合乐趣对话框中PlatformsScreen,我有一个LazyColumn带有RadioButton. 每当选择任何单选按钮时,我都会将所选项目添加到selectedPlatforms其中mutableList:
@Composable
fun PlatformsScreen(
modifier: Modifier = Modifier,
navController: NavController,
viewModel: PlatformsScreenViewModel = hiltViewModel(),
) {
// this is the platforms that I fetch from network
val platforms = viewModel.platforms.observeAsState()
val listState = rememberLazyListState()
//this is the platforms that I selected from the platforms list
val selectedPlatforms by rememberSaveable {
mutableStateOf(mutableListOf<Platform>())
}
DefaultScreenUI(toolbar = {
BottomSheetDialogToolbar(title = "Platforms")
},
floatingActionButton = {
//This is …Run Code Online (Sandbox Code Playgroud) bottom-sheet ×10
android ×5
flutter ×4
kotlin ×2
bottombar ×1
dart ×1
datepicker ×1
dialog ×1
inline ×1
modal-dialog ×1
width ×1
xml ×1