gpt4 book ai didi

Android seekbar解决方案

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:03:04 27 4
gpt4 key购买 nike

是否可以让搜索栏仅在拇指移动时移动。现在搜索栏甚至在手指触摸时移动progressdrawable。我们如何禁用搜索栏的移动progressdrawable 的手指触摸?

谢谢。

最佳答案

我发现 Ravi's solution 的问题是不是很感人移动到当前拇指位置之外仍会导致跳跃。

下面的类解决了这个问题,并用一个小的增量替换了触摸跳转,就像使用箭头键一样。


import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.SeekBar;

/**
* A NoSkipSeekBar is an extension of {@link SeekBar} that prevents jumps in position
* by touching outside the current thumb position. Such touches are replaced by
* an increment or decrement the same as would be achieved using a DPAD's Left or
* Right arrow keys.
*/
public class NoSkipSeekBar extends SeekBar {

public NoSkipSeekBar(Context context) {
super(context);
}

public NoSkipSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
}

public NoSkipSeekBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

private boolean isDragging;

private boolean isWithinThumb(MotionEvent event) {
return getThumb().getBounds().contains((int)event.getX(), (int)event.getY());
}

private void increment(int direction) {
if (direction != 0) {
final KeyEvent key = new KeyEvent(KeyEvent.ACTION_DOWN,
direction < 0 ? KeyEvent.KEYCODE_DPAD_LEFT : KeyEvent.KEYCODE_DPAD_RIGHT);
onKeyDown(key.getKeyCode(), key);
}
}

@Override
public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled() || getThumb() == null) return super.onTouchEvent(event);

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (isWithinThumb(event)) {
isDragging = true;
return super.onTouchEvent(event);
} else {
return true;
}

case MotionEvent.ACTION_UP:
isDragging = false;
if (isWithinThumb(event)) {
return super.onTouchEvent(event);
} else {
final Rect r = getThumb().getBounds();
increment((int)event.getX() - (r.left + r.right) / 2);
return true;
}

case MotionEvent.ACTION_MOVE:
if (!isDragging) return true;
break;

case MotionEvent.ACTION_CANCEL:
isDragging = false;
break;
}

return super.onTouchEvent(event);
}
}

关于Android seekbar解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9787906/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com