Example usage for android.view ViewTreeObserver addOnGlobalFocusChangeListener

List of usage examples for android.view ViewTreeObserver addOnGlobalFocusChangeListener

Introduction

In this page you can find the example usage for android.view ViewTreeObserver addOnGlobalFocusChangeListener.

Prototype

public void addOnGlobalFocusChangeListener(OnGlobalFocusChangeListener listener) 

Source Link

Document

Register a callback to be invoked when the focus state within the view tree changes.

Usage

From source file:eu.inmite.android.lib.validations.form.FormValidator.java

/**
 * Start live validation - whenever focus changes from view with validations upon itself, validators will run.<br/>
  * Don't forget to call {@link #stopLiveValidation(Object)} once you are done.
 *
 * @param target target with views to validate, there can be only one continuous validation per target
 * @param formContainer view that contains our form (views to validate)
 * @param callback callback invoked whenever there is some validation fail, can be null
        //  w ww  . j ava 2s .  c  o  m
 * @throws IllegalArgumentException if formContainer or target is null
 */
public static void startLiveValidation(final Object target, final View formContainer,
        final IValidationCallback callback) {
    if (formContainer == null) {
        throw new IllegalArgumentException("form container view cannot be null");
    }
    if (target == null) {
        throw new IllegalArgumentException("target cannot be null");
    }

    if (sLiveValidations == null) {
        sLiveValidations = new HashMap<Object, ViewGlobalFocusChangeListener>();
    } else if (sLiveValidations.containsKey(target)) {
        // validation is already running
        return;
    }

    final Map<View, FieldInfo> infoMap = FieldFinder.getFieldsForTarget(target);
    final ViewGlobalFocusChangeListener listener = new ViewGlobalFocusChangeListener(infoMap, formContainer,
            target, callback);
    final ViewTreeObserver observer = formContainer.getViewTreeObserver();
    observer.addOnGlobalFocusChangeListener(listener);

    sLiveValidations.put(target, listener);
}

From source file:com.vuze.android.remote.fragment.TorrentListFragment.java

private void setupSideListArea(View view) {
    sideListArea = (LinearLayout) view.findViewById(R.id.sidelist_layout);
    if (sideListArea == null) {
        return;//from   w  w  w .  j  a  v a  2 s.  c  om
    }

    FragmentActivity activity = getActivity();
    if (activity instanceof TorrentViewActivity) {
        ((TorrentViewActivity) activity).setBottomToolbarEnabled(false);
        ((TorrentViewActivity) activity).setShowGlobalActionBar(false);
    }

    if (!AndroidUtils.hasTouchScreen()) {
        // Switch SideList width based on focus.  For touch screens, we use
        // touch events.  For non-touch screens (TV) we watch for focus changes
        ViewTreeObserver vto = sideListArea.getViewTreeObserver();
        vto.addOnGlobalFocusChangeListener(new ViewTreeObserver.OnGlobalFocusChangeListener() {

            @Override
            public void onGlobalFocusChanged(View oldFocus, View newFocus) {

                boolean isChildOfSideList = isChildOf(newFocus, sideListArea);
                boolean isHeader = childOrParentHasTag(newFocus, "sideheader");
                if ((sidelistIsExpanded == null || sidelistIsExpanded) && !isChildOfSideList) {
                    //left focus
                    sidelistInFocus = false;
                    expandSideListWidth(false);
                } else if ((sidelistIsExpanded == null || !sidelistIsExpanded) && isHeader
                        && newFocus != listSideFilter) {
                    sidelistInFocus = true;
                    expandSideListWidth(true);
                }
            }
        });
    }

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
        LayoutTransition layoutTransition = new LayoutTransition();
        layoutTransition.setDuration(300);
        sideListArea.setLayoutTransition(layoutTransition);
    }

    // Could have used a ExpandableListView.. oh well
    setupExpando(view, R.id.sidesort_header, R.id.sidesort_list);
    setupExpando(view, R.id.sidetag_header, R.id.sidetag_list);
    setupExpando(view, R.id.sidefilter_header, R.id.sidefilter_list);
    setupExpando(view, R.id.sideactions_header, R.id.sideactions_list);

    setupSideFilter(view);
    setupSideTags(view);
    setupSideSort(view);
    setupSideActions(view);
}