阻止Android SeekBar在进度更改时绘制多个拇指

时间:2014-12-09 11:42:03

标签: android

我正在为我的appplication创建一个叠加对话框,它的外观如下:

overlay dialog

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/ipTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="0"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@android:color/white" />

    <SeekBar
        android:id="@+id/volumeSeekBar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:layout_weight="1"
        android:focusable="false"
        android:indeterminate="false"
        android:max="63"
        android:mirrorForRtl="false" />

    <TextView
        android:id="@+id/portTv"
        style="@android:style/Theme.Dialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="63"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@android:color/white" />

</LinearLayout>

代码:

if (alertDialogLayout == null) {
            alertDialogLayout = View.inflate(getApplicationContext(), R.layout.volume_dialog, null);    

            WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);

            DisplayMetrics dm = new DisplayMetrics();
            wm.getDefaultDisplay().getMetrics(dm);

            WindowManager.LayoutParams wmlp = new LayoutParams();
            wmlp.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
            wmlp.width = dm.widthPixels / 5;
            wmlp.height = LayoutParams.WRAP_CONTENT;
            wmlp.y += 10;
            wmlp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
            wmlp.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL;
            wm.addView(alertDialogLayout, wmlp);
        }

        if (volumeBar == null) {
            volumeBar = (SeekBar) alertDialogLayout.findViewById(R.id.volumeSeekBar);
            volumeBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//Do some stuff
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) { }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) { }
            });
        }

为什么SeekBar会添加多个拇指?随着我拖动它,更多的拇指出现,直到它完全充满它们。我已经检查过了 - OnSeekBarChangeListener只分配了一次。代码/布局中是否有任何错误,我缺少并导致如此奇怪的视觉行为?

谢谢!

1 个答案:

答案 0 :(得分:0)

我认为您的问题不是多个拇指,而是多个SeekBars,多个alertDialogLayout,每个人都有一个SeekBar,或者每个人创建的多个对象会创建一个alertDialogLayout ,首先,请确保您只启动alertDialogLayout一次,并且仅启动SeekBar一次,同时确保您不是(某些方式)发起alertDialogLayoutSeekBar SeekBar进度更改,并暂时测试所有这些,只需将alertDialogLayoutSeekBar声明更改为static,看看这种奇怪的行为是否会消失。