将限制设置为搜索栏的拖动范围

时间:2017-11-27 07:09:03

标签: android seekbar android-seekbar

我有一个搜索栏列表。我想要的是设置搜索栏的限制。虽然我是通过使用seekBar.setMax(anyIntValue);完成的,但这会设置整个搜索栏值。但我想要的是例如用户选择第一个搜索栏到90%,第二个搜索栏应该只拖到10%。

这是我的代码:

holder.shelfShareSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                holder.percentTextView.setText(String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                remainingShelfShare = 100 - shelfShareSum;
                seekBar.setMax(remainingShelfShare);
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                list.get(position).setSetSlider(seekBar.getProgress());
                shelfShareSum = 0;

                productShelfShareMap.put(((Products)list.get(position)).getProductID(), Integer.parseInt(holder.percentTextView.getText().toString()));
                for (HashMap.Entry<Integer, Integer> entry : productShelfShareMap.entrySet()){
                    shelfShareSum += entry.getValue();
                }

            }
        });

1 个答案:

答案 0 :(得分:0)

这里的代码对我来说很合适。

seekBar1 = (SeekBar)findViewById(R.id.seekBar1);
seekBar2 = (SeekBar)findViewById(R.id.seekBar2);

在seekBar上设置seekBarChangeListener

 //seekBar1
 seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

           if(seekBar2.getProgress()>0){
               seekBar2.setProgress(0);
               seekBar2.setFocusable(false);
           }
        }
    });

//seekBar2


 seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

现在在seekBar2上添加setOnTouchListener,以便在seekBar2中停止跟踪触摸事件

  seekBar2.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            int firstProgress = (100 - seekBar1.getProgress()); //100 max range of seekBar
            int secondProgress = seekBar2.getProgress();
            return secondProgress >= firstProgress;
        }
    });

我希望这段代码很有帮助。