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

Android 防止双击时底部工作表对话框片段显示两次,android,android-studio,bottom-sheet,android-bottomsheetdialog,Android,Android Studio,Bottom Sheet,Android Bottomsheetdialog,您好,我有一个底部工作表对话框片段,当单击recyclerview中的项目时显示。底部工作表实现的显示在recyclerview的适配器中。我遇到的问题是,当您快速双击项目以显示两次底部工作表时,是否有办法将点击限制为仅点击一次,或者在通过检查显示底部工作表对话框片段时不再次显示底部工作表对话框片段 下面是我如何在recyclerview中显示项目点击的底部工作表 @Override public void onBindViewHolder(@NonNull final MyViewH

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

下面是我如何在recyclerview中显示项目点击的底部工作表


@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的答案,方法如下

声明一个布尔值以显示是否显示底部工作表

 private boolean isBottomSheetShowing = false;

在下表中实现


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;
                }


现在出现的问题是,当我单击项目时,有一段时间什么都没有发生,然后再次单击该项目后,它会显示

有很多方法可以做到这一点,其中一种方法是将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());
            }
        });

    }

有很多方法可以做到这一点,其中一种方法是将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());
            }
        });

    }

如果使用外部库是一个选项,那么您可以使用
RxView
from来控制它,例如

RxView.clicks(myViewHolder.cardView)
.节气门(2,时间单位:秒)
.subscribe{}

此处,
throttle
是禁用单击的时间长度。

如果使用外部库是一个选项,则可以使用
RxView
从来控制此操作,例如

RxView.clicks(myViewHolder.cardView)
.节气门(2,时间单位:秒)
.subscribe{}

这里,,
throttle
是禁用单击的时间长度。

是否可以使用BottomSheetBehavior上述方法对BottomSheetBehavior的功能没有任何影响是否可以使用BottomSheetBehavior上述方法对BottomSheetBehavior的功能没有任何影响这是库还是捆绑的对于Androides,这是一个库:这是不相关的答案,RxBinding不在本例中使用。这是一个库还是与Androides捆绑在一起?这是一个库:这是不相关的答案,RxBinding不在本例中使用