RecyclerView完美條目點擊效果selector

通過變更條目背景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效果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章