通过变更条目背景Drawable做selector达不到你们美丽UI工程师的要求?(通过变更背景的selector无法达到需求),给你一个条目容器,
/**
* Jay
* 可以感知press和 upPress事件的ConstraintLayout
*/
public class PerceptPressedConstraintLayout extends ConstraintLayout {
private StatePressedCallback mCallBack;
public PerceptPressedConstraintLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public PerceptPressedConstraintLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
mCallBack.pressed(true);
} else if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
mCallBack.pressed(false);
}
return super.onTouchEvent(event);
}
public interface StatePressedCallback {
void pressed(boolean isPressed);
}
public void setEventDownCallback(StatePressedCallback callback) {
mCallBack = callback;
}
}
使用姿势:
为什么叫"取消按下"?
因为MotionEvent.ACTION_UP和CANCEL都要取消hover效果