MaterialRangeBar:setOnRangeBarChangeListener 方法

MaterialRangeBar: setOnRangeBarChangeListener method

我正在尝试在 android 应用程序中实现一个选项,用户可以使用范围栏来设置他们希望应用程序寻找附近朋友的半径。对于范围栏,我正在使用这个库 GitHub page. 这也是来自我的 activity 的范围栏的 .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"/>

我想做什么?

我想要在用户尝试移动选择器时接收消息。这是对此做出响应的功能。

RangeBar radiusRangeBar;
AlertDialog alertDialog;
AlertDialog.Builder builder;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.activity_settings, container, false);
radiusRangeBar = (RangeBar) view.findViewById(R.id.radiusRangeBar);

radiusRangeBar.setOnRangeBarChangeListener(new RangeBar.OnRangeBarChangeListener() {
            @Override
            public void onRangeChangeListener(RangeBar rangeBar, int leftPinIndex, int rightPinIndex, String leftPinValue, String rightPinValue) {
                builder = new AlertDialog.Builder(getActivity());
                builder.setMessage("You are about to set the Nearby friends radius on " + rightPinValue + "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();
            }
        });
}

怎么了? - 由于这是 GitHub 页面中文档中提到的唯一功能,我尝试实现它,但它不适合我的设置,因为它在我滚动时显示消息,我希望只有当我滚动时它才会发生将停止滚动。 这是可行的还是我必须尝试另一种方法来通知用户他的操作。

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

我将分享第二种方法的解决方案。 总的来说,只有在范围一段时间未更改(我们将持续时间指定为 300 毫秒)的情况下,您才需要采取一些措施。因此你可以使用handler.postDelayed()机制来克服这个问题。

这样声明 handlershowDialogRunnable 字段:


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

这将确保,只要 onRangeChangeListener() 没有在 300 毫秒的 window 内触发,就会显示对话框。

我补充了 azizbekian 的答案。 我已经更新了我的代码并张贴在这里,以便将来每个人都能够快速轻松地解决他的问题。

现在是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);
        }
    });

我创建了字符串变量,它通过移动范围栏选择器进行初始化。

然后,如果没有任何操作,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));
            }
        };