Custom Checkable View which responds to Selector

前端 未结 6 478
生来不讨喜
生来不讨喜 2021-01-30 23:11

I have a group of FrameLayout which I want to be checkable/selectable,

That is, after a click I would like the FrameLayout to display as

6条回答
  •  不思量自难忘°
    2021-01-30 23:43

    Adding the following code to a Checkable class allows Selectors to work:

    private static final int[] CheckedStateSet = {
        android.R.attr.state_checked,
    };
    
    @Override
    protected int[] onCreateDrawableState(int extraSpace) {
        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        if (isChecked()) {
            mergeDrawableStates(drawableState, CheckedStateSet);
        }
        return drawableState;
    }
    
    @Override
    public boolean performClick() {
        toggle();
        return super.performClick();
    }
    

提交回复
热议问题