gpt4 book ai didi

android - 双击时防止 Bottom Sheet 对话框 fragment 显示两次

转载 作者:行者123 更新时间:2023-12-04 23:49:59 24 4
gpt4 key购买 nike

您好,我有一个 Bottom Sheet 对话框 fragment ,当单击 recyclerview 中的项目时会显示该 fragment 。 Bottom Sheet 实现的显示在 recyclerview 的适配器中。遇到的问题是,当您快速双击项目以显示 Bottom Sheet 时,它会显示两次,有没有办法将点击限制为仅单击一次,或者在显示 Bottom Sheet 对话框 fragment 时不再次显示通过检查

这是在recyclerview中单击项目时显示 Bottom Sheet 的方式


@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());
}
});

}


我尝试通过执行以下操作来使用 Muhannad Fakhouri 的答案

声明一个 bool 值以显示 BottomSheet 是否显示
 private boolean isBottomSheetShowing = false;


在 Bottom Sheet 中实现

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);
itemBottomSheet.show(fragmentManager, itemBottomSheet.getTag());

}else{
isBottomSheetShowing = false;
}


现在出现的问题是,有一段时间当我单击该项目时完全没有任何 react ,然后在我再次单击该项目后它显示

最佳答案

我正在尝试检查 fragment 是否已添加到 fragment 支持管理器中,如果添加则不再显示。
这是代码: (将这段代码放在同步块(synchronized block)中以获得更好的功能)

        for(Fragment fragment: activity.getSupportFragmentManager().getFragments()){
if(fragment instanceof BottomSheetFragment)
return;
}
BottomSheetFragment bottomSheetFragment = BottomSheetFragment.newInstance();
bottomSheetFragment.show(activity.getSupportFragmentManager(), "bottom_sheet_fragment");
如果出现任何异常,请发表评论,我会尽力修复它。

关于android - 双击时防止 Bottom Sheet 对话框 fragment 显示两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62284444/

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