Make RelativeLayout checkable

后端 未结 4 1121
长发绾君心
长发绾君心 2021-01-03 04:53

I try to write my own CheckBox using RelativeLayout with TextView and FrameLayout (with selector on background) inside.

相关标签:
4条回答
  • 2021-01-03 05:27

    I just need override method:

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

    Now all works.

    0 讨论(0)
  • 2021-01-03 05:42

    Nik's answer is partially right for me, you need added android.R.attr.state_checkable into the STATE_CHECKABLE array, and set the corresponding lenth (2), or else it fails to work.

    private static final int[] CHECKED_STATE_SET = {android.R.attr.state_checked, android.R.attr.state_checkable};
    
    @Override
    protected int[] onCreateDrawableState(int extraSpace)
    {
        int[] result = super.onCreateDrawableState(extraSpace + CHECKED_STATE_SET.length);
        if(mChecked) mergeDrawableStates(result, CHECKED_STATE_SET);
        return result;
    }
    
    0 讨论(0)
  • 2021-01-03 05:45

    There seem to be two UI patterns for multi-select lists in Holo (in the context of ActionMode): using checkboxes or highlighting.

    If all you need is highlighting, you can use this simpler method:

    Make your list item layout use for the root a class like this one:

    public class CheckableRelativeLayout extends RelativeLayout implements Checkable {
    private static final int[] STATE_CHECKABLE = {R.attr.state_pressed};
    boolean checked = false;
    
    public void setChecked(boolean checked) {
        this.checked = checked;
        refreshDrawableState();
    }
    
    public boolean isChecked() {
        return checked;
    }
    
    public void toggle() {
        setChecked(!checked);
    }
    
    @Override
    protected int[] onCreateDrawableState(int extraSpace) {
        int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        if (checked) mergeDrawableStates(drawableState, STATE_CHECKABLE);
    
        return drawableState;
    }
    }
    

    and make sure the root element in your list item layout uses

    android:background="?android:attr/listChoiceBackgroundIndicator"
    

    I know this is not really an answer to your question (you wanted an actual checkbox), but I haven't seen this documented anywhere.

    The ListView uses the Checkable interface to determine whether it can rely on the list item element to display the checkable state or if it should try to display it itself.

    0 讨论(0)
  • 2021-01-03 05:45

    http://developer.android.com/reference/android/view/View.html#setClickable%28boolean%29

    public MyCheckbox(Context context)
    {
        this(context, null);
        this.setClickable(true);
    }
    
    public MyCheckbox(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        this.setClickable(true);
    //......
    }
    
    0 讨论(0)
提交回复
热议问题