ToggleView.java 2.99 KB
Newer Older
konghaorui committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
package com.yidianling.avchatkit.common.widgets;

import android.view.View;
import android.view.ViewGroup;

/**
 * Created by hzlichengda on 14-3-14.
 * if you want to use this parentView with an inner parentView inside ,the inner parentView's id must be R.id.childView
 */
public class ToggleView {

    private View parentView = null;
    private View childView = null;
    private ToggleState state = ToggleState.DISABLE;
    private ToggleListener listener = null;

    public ToggleView(View parentView, ToggleState initState, ToggleListener listener) {
        this.parentView = parentView;
        this.state = initState;
        this.listener = listener;
        init();
    }

    private void init() {
        if (parentView != null) {
            parentView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onToggleStateChange();
                }
            });
            if (parentView instanceof ViewGroup) {
                ViewGroup viewGroup = (ViewGroup) parentView;
                childView = viewGroup.getChildAt(0);
            }
            toggle(state);
        }
    }

    public void toggle(ToggleState state) {
        switch (state) {
            case DISABLE:
                disable(false);
                break;
            case OFF:
                off(false);
                break;
            case ON:
                on(false);
                break;
        }
    }

    private void onToggleStateChange() {
        switch (state) {
            case DISABLE:
                disable(true);
                break;
            case OFF:
                on(true);
                break;
            case ON:
                off(true);
                break;
        }
    }

    public void on(boolean callback) {
        state = ToggleState.ON;

        parentView.setEnabled(true);
        parentView.setSelected(true);

        if (childView != null) {
            childView.setEnabled(true);
            childView.setSelected(true);
        }

        if (listener != null && callback)
            listener.toggleOn(parentView);
    }

    public void off(boolean callback) {
        state = ToggleState.OFF;

        parentView.setEnabled(true);
        parentView.setSelected(false);

        if (childView != null) {
            childView.setEnabled(true);
            childView.setSelected(false);
        }

        if (listener != null && callback)
            listener.toggleOff(parentView);
    }

    public void disable(boolean callback) {
        state = ToggleState.DISABLE;

        parentView.setSelected(false);
        parentView.setEnabled(false);

        if (childView != null) {
            childView.setSelected(false);
            childView.setEnabled(false);
        }

        if (listener != null && callback)
            listener.toggleDisable(parentView);
    }

    public void enable() {
        off(false);
    }

    public boolean isEnable() {
        return state != ToggleState.DISABLE;
    }

}