warkiz / IndicatorSeekBar

A custom SeekBar on Android, which can be changed the size ,color , thumb drawable , tick drawable , tick text and indicator , also , will show an indicator view with progress above SeekBar when seeking. https://github.com/warkiz/IndicatorSeekBar
Apache License 2.0
2.17k stars 419 forks source link

IndicatorSeekBar的onMeasure被调用时会设置错误的进度 #216

Open zilchzz opened 4 years ago

zilchzz commented 4 years ago

1,使用下面的布局复现问题,主要意思是使用一个TextView跟IndicatorSeekBar右对齐:

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            style="@style/subtitle_text_style"
            android:id="@+id/tmp4SeekBar"
            android:text="4. seek_smoothly=true, show_tick_marks_type=oval,\n customTickTexts,thumb_adjust_auto=false" />

        <com.warkiz.widget.IndicatorSeekBar
            android:id="@+id/custom_text"
            android:layout_width="0dp"
            app:layout_constraintTop_toBottomOf="@id/tmp4SeekBar"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_height="wrap_content"
            app:isb_progress="30"
            android:layout_marginTop="10dp"
            app:isb_seek_smoothly="true"
            app:isb_thumb_adjust_auto="false"
            app:layout_constraintEnd_toEndOf="parent"
            app:isb_show_indicator="rounded_rectangle"
            app:isb_show_tick_marks_type="oval"
            app:isb_ticks_count="7"
            app:isb_track_progress_color="@color/color_blue" />

        <TextView
            app:layout_constraintTop_toBottomOf="@id/custom_text"
            app:layout_constraintRight_toRightOf="@id/custom_text"
            android:layout_marginTop="10dp"
            android:id="@+id/tmp4SeekBarTv"
            android:layout_marginEnd="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </androidx.constraintlayout.widget.ConstraintLayout>

2,给IndicatorSeekBar设置OnSeekChangeListener:

    //customTickTexts
    IndicatorSeekBar seekBar = root.findViewById(R.id.custom_text);
    final TextView tmp4SeekBarTv = root.findViewById(R.id.tmp4SeekBarTv);
    seekBar.customTickTexts(arr);
    seekBar.setOnSeekChangeListener(new OnSeekChangeListener() {
        @Override
        public void onSeeking(SeekParams seekParams) {
            tmp4SeekBarTv.setText(seekParams.progress + "");
        }

        @Override
        public void onStartTrackingTouch(IndicatorSeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(IndicatorSeekBar seekBar) {

        }
    });

3,这时候滑动滑块,会使得seekBar自动设置为固定的档次,比如我设置两个滑块,当前滑到24%,则滑块会自动置为50%的位置;

4,如果不在onSeeking内设置TextView的值,则不会出现这个问题;

5,debug之后发现tmp4SeekBarTv设置text之后会使得IndicatorSeekBar调用onMeasure,然后方法内会重新设置进度;

6,想要避免这个问题,可以直接使用LinearLayout嵌套IndicatorSeekBar跟TextView;