Seekbar在RecyclerView

时间:2015-09-29 08:59:02

标签: android android-recyclerview seekbar

问题: 我有很多搜索栏(由RecyclerView.Adapter管理),当我滚动视图,并且不再显示特定的搜索栏时,此搜索栏中的进度自身设置为0(我可以在OnSeekBarChangeListner中看到这一点)。我想知道为什么。

预期行为: 当用户不通过滑动搜索栏来改变值时,进度不应该改变。

我现在的解决方法是检查是否显示了搜索栏,而不是保存值。

onBindViewHolder

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int positi
    final DMXChannel currentChannel = channels.get(position);

   ... // Not relevant
else if (holder instanceof ViewHolderSlider) {
        ((ViewHolderSlider) holder).container.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (listner != null)
                    listner.onLongItemClicked(currentChannel.getIndex(), v);
                return true;
            }
        });
        final ViewHolderSlider holderSlider=((ViewHolderSlider) holder);
                ((ViewHolderSlider) holder).tvPosition.setText("" + (currentChannel.getIndex() + 1));
        int value =currentChannel.getValue();
        holderSlider.slider.setProgress(value);
        ((ViewHolderSlider) holder).tvValue.setText("" + currentChannel.getValue());
        ((ViewHolderSlider) holder).slider.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                holderSlider.tvValue.setText("" + progress);
                if(position!=RecyclerView.NO_POSITION) {
                    if(seekBar.isShown())
                    currentChannel.setValue(progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }
}

我的ViewHolder for seekbars

public static class ViewHolderSlider extends RecyclerView.ViewHolder {
    public TextView tvPosition;
    public TextView tvValue;
    public CardView container;
    public VerticalSeekBar slider;
    public ViewHolderSlider(View v) {
        super(v);
        slider= (VerticalSeekBar)v.findViewById(R.id.slider);
        Drawable thumb = MyApp.getContext().getResources().getDrawable(R.drawable.ic_add_white_24dp);
        slider.setThumb(thumb);
        thumb.mutate().setAlpha(0);
        container= (CardView)v.findViewById(R.id.rootView);
        tvPosition= (TextView) v.findViewById(R.id.tv_channel_number);
        tvValue = (TextView) v.findViewById(R.id.tv_value);
    }
}

1 个答案:

答案 0 :(得分:2)

您必须检查用户是否进行了更改。您可以使用“fromUser”变量执行此操作:

((ViewHolderSlider) holder).slider.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        holderSlider.tvValue.setText("" + progress);
        if(position!=RecyclerView.NO_POSITION) {
            if(seekBar.isShown() && fromUser)
                currentChannel.setValue(progress);
        }
    }