package com.allynav.digitalconstructionsystem.view.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import androidx.annotation.Nullable;
import com.allynav.digitalconstructionsystem.R;
public class SetItemSeekbar extends LinearLayout {
private TextView mTextView, barnum;
private SeekBar mSeekBar;
public SetItemSeekbar(Context context) {
super(context);
init(context, null);
}
public SetItemSeekbar(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public SetItemSeekbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
View view = View.inflate(context, R.layout.view_set_seekbar, this);
mTextView = view.findViewById(R.id.tv_left_setitem);
barnum = view.findViewById(R.id.tv_right_barnum);
mSeekBar = view.findViewById(R.id.sb_setitem);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SetItemSeekbar);
//关联text
String text = ta.getString(R.styleable.SetItemSeekbar_text);
if (text != null) mTextView.setText(text);
int max = ta.getInt(R.styleable.SetItemSeekbar_max, 100);
mSeekBar.setMax(max);
int pro = ta.getInt(R.styleable.SetItemSeekbar_progress, 0);
mSeekBar.setProgress(pro);
mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
barnum.setText(seekBar.getProgress() + "");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
ta.recycle();
}
public double getBarProgress() {
return Double.parseDouble(barnum.getText().toString());
}
public void setBarProgress(int progress) {
mSeekBar.setProgress(progress);
}
}