当我双击时,防止底部工作表对话框片段显示两次

Emm*_*ngo 5 android android-studio bottom-sheet android-bottomsheetdialog

您好,我有一个底部工作表对话框片段,当单击回收器视图中的项目时会显示该片段。底部表单实现的显示位于回收视图的适配器中。我遇到的问题是,当您快速双击项目以显示底部工作表时,它会显示两次,是否有一种方法可以将点击限制为仅一次点击,或者在显示底部工作表对话框片段时不再显示底部工作表对话框片段通过检查

以下是如何在回收视图中显示项目单击的底部工作表


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

                }else{
                    isBottomSheetShowing = false;
                }

Run Code Online (Sandbox Code Playgroud)

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

Muh*_*uri 2

有很多方法可以做到这一点,一种方法是将 BottomSheetFragment 保存在字段中,并在显示之前检查它是否显示

BottomSheetFragment bottomSheetFragment = new BottomSheetFragment();

@Override
    public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, final int position) {


        myViewHolder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(bottomSheetFragment.isAdded()) return
                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.showNow(fragmentManager, bottomSheetFragment.getTag());
            }
        });

    }
Run Code Online (Sandbox Code Playgroud)