我有三种类型的类别,我想从左向右滑动选择每种类型,就像滑动选择器一样。任何帮助将不胜感激

最佳答案

使用搜索栏满足此要求。

在xml文件中创建一个搜索栏。

 <SeekBar
        android:id="@+id/seekbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />


使用setOnSeekBarChangeListener侦听搜索中的更改,

SeekBar skbarVisibleWithin = (SeekBar) findViewById(R.id.seekbar);
        skbarVisibleWithin.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            public void onStopTrackingTouch(SeekBar seekBar) {
                /**
                 * if the progress is less than 25 than we should set
                 * the progress to 0, else if the progress is less than
                 * 75 then we should set the progress as 50 else set the
                 * progress as 100;
                 **/
                int progress = seekBar.getProgress();
                if (progress < 25) {
                    seekBar.setProgress(PROGRESS_STAGE_1);
                }
                else if (progress < 75) {
                    seekBar.setProgress(PROGRESS_STAGE_2);
                }
                else {
                    seekBar.setProgress(PROGRESS_STAGE_3);
                }
            }

            public void onStartTrackingTouch(SeekBar seekBar) {}

            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (progress == PROGRESS_STAGE_1) {
                    //TODO:
                }
                else if (progress == PROGRESS_STAGE_2) {
                    //TODO:
                }
                else if (progress == PROGRESS_STAGE_3) {
                    //TODO:
                }
            }
        });


在onProgessChanged中,手动设置进度。

10-04 20:14