android seekbar阻挡父viewgroup的长按事件
直接上代码public class TouchFrameLayout extends FrameLayout {//继承啥viewgroup都行private OnLongClickListener longclickListener;public void setLongclickListener(OnLongClickListener longclickListen...
·
直接上代码
public class TouchFrameLayout extends FrameLayout {//继承啥viewgroup都行
private OnLongClickListener longclickListener;
public void setLongclickListener(OnLongClickListener longclickListener) {
this.longclickListener = longclickListener;
}
public TouchFrameLayout(Context context) {
super(context);
}
public TouchFrameLayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public TouchFrameLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
long time = 0;
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
time = new Date().getTime();
return super.onInterceptTouchEvent(ev);
} else {
long cureent = new Date().getTime() - time;
if (cureent < 1000) {
return false;
} else {//大于1000视为长按
longclickListener.onLongClick(this);//长按事件
return true;
}
}
}
}
代码中或者xml中初始化都可以
TouchFrameLayout contentLayout = itemView.findViewById(R.id.contentFrameLayout);
contentLayout.setLongclickListener(listener);//此处设置长按监听
View.OnLongClickListener listener = v -> {//使用view的长按监听接口
//做你的事件处理
return true;
};
这样父view的长按事件就不会被seekbar挡住 seekbar中的点击拖拽依然可用哦
更多推荐
已为社区贡献4条内容
所有评论(0)