Java MaterialRangeBar:setOnRangeBarChangeListener方法

Java MaterialRangeBar:setOnRangeBarChangeListener方法,java,android,android-layout,android-view,ratingbar,Java,Android,Android Layout,Android View,Ratingbar,我正在尝试在android应用程序中实现一个选项,用户可以使用范围栏设置他们希望应用程序寻找附近朋友的半径。对于我正在使用的范围栏,这里也是活动中范围栏的.xml代码的一部分 <com.appyvet.materialrangebar.RangeBar android:layout_marginEnd="4dp" android:layout_width="wrap_content" android:layout_height="60dp"

我正在尝试在android应用程序中实现一个选项,用户可以使用范围栏设置他们希望应用程序寻找附近朋友的半径。对于我正在使用的范围栏,这里也是活动中范围栏的.xml代码的一部分

<com.appyvet.materialrangebar.RangeBar
        android:layout_marginEnd="4dp"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:id="@+id/radiusRangeBar"
        app:mrb_rangeBar="false"
        app:mrb_tickStart="1"
        app:mrb_tickEnd="5"
        app:mrb_tickInterval="1"
        app:mrb_pinRadius="8dp"
        app:mrb_pinMaxFont="8sp"
        app:mrb_barWeight="1dp"
        app:mrb_tickColor="@color/colorPrimary"
        app:mrb_connectingLineColor="@color/colorPrimaryDark"
        app:mrb_connectingLineWeight="3dp"
        app:mrb_pinColor="@color/switchColor"
        app:mrb_selectorColor="@color/switchColor"
        app:mrb_selectorSize="10dp"/>
怎么了? -由于这是GitHub页面文档中提到的唯一一个函数,我尝试实现它,但它不适合我的设置,因为它在我滚动时显示消息,我希望只有在我停止滚动时才会出现。
这是否可行,或者我必须尝试另一种方法来通知用户他的操作

我已经检查了自定义小部件的源代码。不存在您感兴趣的任何回调,这意味着您有两个选择:要么更改源本身,要么应用解决方案

我将分享第二种方法的解决方案。 总的来说,只有在范围有一段时间没有改变的情况下(让我们将持续时间指定为300ms),才需要执行一些操作。因此,您可以使用
handler.postDelayed()
机制来克服这个问题

声明
处理程序
showDialogRunnable
字段如下:


    private final Handler handler = new Handler();

    private final Runnable showDialogRunnable = new Runnable() {
        @Override
        public void run() {
            showDialog();
        }
    }
然后在侦听器内执行以下更改:


    radiusRangeBar.setOnRangeBarChangeListener(new RangeBar.OnRangeBarChangeListener() {
        @Override
        public void onRangeChangeListener(RangeBar rangeBar, int leftPinIndex, int rightPinIndex, String leftPiString rightPinValue) {
            // first remove the previous action if it exists
            handler.removeCallbacks(showDialogRunnable);
            // schedule a new action to take place in 300ms
            handler.postDelayed(showDialogRunnable, 300);
        }
    });

这将确保只要在300ms的窗口中未触发
onRangeChangeListener()
,就会显示对话框。

I从中添加答案。 我已经更新了我的代码并发布在这里,这样将来每个人都能够快速轻松地解决自己的问题

这是现在的
setOnRangeBarChangeListener

 radiusRangeBar.setOnRangeBarChangeListener(new RangeBar.OnRangeBarChangeListener() {
        @Override
        public void onRangeChangeListener(RangeBar rangeBar, int leftPinIndex, int rightPinIndex, String leftPinValue, String rightPinValue) {
            rangeBarValue = rightPinValue;
            // first remove the previous action if it exists
            handler.removeCallbacks(showDialogRunnable);
            // schedule a new action to take place in 700ms
            handler.postDelayed(showDialogRunnable, 700);
        }
    });
我已经创建了字符串变量,它通过移动rangebar选择器初始化

然后,如果
handler.postDelayed(showDialogRunnable,700)上没有执行任何操作将触发

下面是这个函数的代码

private final Handler handler = new Handler();
final Runnable showDialogRunnable = new Runnable() {
            @Override
            public void run() {
                builder = new AlertDialog.Builder(getActivity());
                builder.setMessage("You are about to set the Nearby friends radius on " + rangeBarValue + "km") ;
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        privateAccountsSwitch.setChecked(true);
                    }
                });
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        privateAccountsSwitch.setChecked(false);
                    }
                });
                alertDialog = builder.create();
                alertDialog.show();
                alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.alertDialogPositiveButton));
                alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.alertDialogNegativeButton));
            }
        };

您需要获取textview并显示那些范围是的,我尝试过,但现在我希望用户能够使用对话框中的肯定按钮确认其操作。然后将该范围设置在肯定的Listener onclick Event内每次滚动后都会创建对话框,因此我认为您的建议对我的情况没有用处不管怎样,谢谢。这太好了。你说服了我。非常感谢。我马上就来。
private final Handler handler = new Handler();
final Runnable showDialogRunnable = new Runnable() {
            @Override
            public void run() {
                builder = new AlertDialog.Builder(getActivity());
                builder.setMessage("You are about to set the Nearby friends radius on " + rangeBarValue + "km") ;
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        privateAccountsSwitch.setChecked(true);
                    }
                });
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        privateAccountsSwitch.setChecked(false);
                    }
                });
                alertDialog = builder.create();
                alertDialog.show();
                alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.alertDialogPositiveButton));
                alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.alertDialogNegativeButton));
            }
        };