我正在我的应用程序中实现一个seekBar,我想让它基于滑动而不是基于点击。但是一旦用户完成滑动,而不是改变进度,seekBar必须等待事件完成,并且基于事件的响应,seekBar中的进度将相应地改变为滑动值。
The flow will be like:- slide the seekBar--on sliding finished wait for an event to be finished--once the event finished, make the corresponding progress change based on the sliding.
Below I am posting my code.
The custom seekBar class:
public class Slider extends SeekBar {
private Drawable mThumb;
public Slider(Context context) {
super(context);
}
public Slider(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void setThumb(Drawable thumb) {
super.setThumb(thumb);
mThumb = thumb;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (event.getX() >= mThumb.getBounds().left
&& event.getX() <= mThumb.getBounds().right
&& event.getY() <= mThumb.getBounds().bottom
&& event.getY() >= mThumb.getBounds().top) {
super.onTouchEvent(event);
} else {
return false;
}
} else if (event.getAction() == MotionEvent.ACTION_UP) {
return false;
} else {
super.onTouchEvent(event);
}
return true;
}}
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Fragment where the seekBar is implemented:
viewHolder.dimming.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int originalProgress;
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
originalProgress = seekBar.getProgress();
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(viewHolder.getAdapterPosition()!=RecyclerView.NO_POSITION)
{
if(seekBar.isShown() && fromUser)
{
progress= (progress/10)*10;
//ProgressUtil.showProgressDialog(progressDialog);
Log.d("Seek", String.valueOf(progress));
viewHolder.dimming.setEnabled(false);
handleDCURequest.slc_dimming((Context)myContext,slcIDVal,progress, originalProgress,dcuIDVal,viewHolder.dimming);
//adapter.getRef(viewHolder.getAdapterPosition()).child("Percentage").setValue(progress);
//viewHolder.showProgress.setText(progress+"%");
}
}
}
});
I tried my best,but failed. Please do help. Thanks in advance.