Example usage for android.view View getOnFocusChangeListener

List of usage examples for android.view View getOnFocusChangeListener

Introduction

In this page you can find the example usage for android.view View getOnFocusChangeListener.

Prototype

public OnFocusChangeListener getOnFocusChangeListener() 

Source Link

Document

Returns the focus-change callback registered for this view.

Usage

From source file:ti.modules.titanium.ui.widget.tableview.TiTableView.java

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    // To prevent undesired "focus" and "blur" events during layout caused
    // by ListView temporarily taking focus, we will disable focus events until
    // layout has finished.
    // First check for a quick exit. listView can be null, such as if window closing.
    if (listView == null) {
        super.onLayout(changed, left, top, right, bottom);
        return;//  ww  w  .  ja  v a 2  s . c o m
    }
    OnFocusChangeListener focusListener = null;
    View focusedView = listView.findFocus();
    if (focusedView != null) {
        OnFocusChangeListener listener = focusedView.getOnFocusChangeListener();
        if (listener != null && listener instanceof TiUIView) {
            focusedView.setOnFocusChangeListener(null);
            focusListener = listener;
        }
    }

    super.onLayout(changed, left, top, right, bottom);

    if (proxy != null) {
        if (changed) {
            TiUIHelper.firePostLayoutEvent(proxy.peekView());
        }
    }

    // Layout is finished, re-enable focus events.
    if (focusListener != null) {
        focusedView.setOnFocusChangeListener(focusListener);
        // If the configuration changed, we manually fire the blur event
        if (changed) {
            focusListener.onFocusChange(focusedView, false);
        }
    }
}

From source file:com.owen.tvrecyclerview.widget.TvRecyclerView.java

@Override
public void onChildAttachedToWindow(View child) {
    if (!ViewCompat.hasOnClickListeners(child)) {
        child.setOnClickListener(mItemListener);
    }//from  w w w  .  j a v a2s.  c  o m
    if (null == child.getOnFocusChangeListener()) {
        child.setOnFocusChangeListener(mItemListener);
    }
}

From source file:ti.modules.titanium.ui.widget.collectionview.TiCollectionView.java

public TiCollectionView(TiViewProxy proxy, Activity activity) {
    super(proxy);

    // initializing variables
    sections = Collections.synchronizedList(new ArrayList<AbsListSectionProxy>());
    itemTypeCount = new AtomicInteger(CUSTOM_TEMPLATE_ITEM_TYPE);
    defaultTemplateBinding = defaultTemplateKey;
    defaultTemplate.setType(BUILT_IN_TEMPLATE_ITEM_TYPE);
    processTemplates(null);//from  w  w  w  . j a  va 2s . c  o  m

    caseInsensitive = true;
    ignoreExactMatch = false;

    // handling marker
    HashMap<String, Integer> preloadMarker = ((AbsListViewProxy) proxy).getPreloadMarker();
    if (preloadMarker != null) {
        setMarker(preloadMarker);
    } else {
        resetMarker();
    }

    final KrollProxy fProxy = proxy;
    layoutManager = new TiGridLayoutManager(activity);

    // trick to get scrollbar to be initialized!
    mRecyclerView = new FastScrollRecyclerView(activity, null,
            Resources.getSystem().getIdentifier("listViewStyle", "attr", "android")) {
        // private boolean viewFocused = false;
        private boolean selectionSet = false;

        @Override
        protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
            // if ((Build.VERSION.SDK_INT >= 18 && !changed && viewFocused))
            // {
            // viewFocused = false;
            // super.onLayout(changed, left, top, right, bottom);
            // return;
            // }
            // Starting with API 21, setSelection() triggers another layout
            // pass, so we need to end it here to prevent
            // an infinite loop
            if (Build.VERSION.SDK_INT >= 21 && selectionSet) {
                selectionSet = false;
                return;
            }
            OnFocusChangeListener focusListener = null;
            View focusedView = findFocus();
            int cursorPosition = -1;
            if (focusedView != null) {
                if (focusedView instanceof EditText) {
                    cursorPosition = ((EditText) focusedView).getSelectionStart();
                }
                OnFocusChangeListener listener = focusedView.getOnFocusChangeListener();
                if (listener != null && listener instanceof TiUIView) {
                    focusedView.setOnFocusChangeListener(null);
                    focusListener = listener;
                }
            }
            if (focusedView != null) {
                setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
            }
            super.onLayout(changed, left, top, right, bottom);
            setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);

            if (changed) {
                TiUIHelper.firePostLayoutEvent(TiCollectionView.this);
            }

            // Layout is finished, re-enable focus events.
            if (focusedView != null) {
                // If the configuration changed, we manually fire the blur
                // event
                if (changed) {
                    focusedView.setOnFocusChangeListener(focusListener);
                    if (focusListener != null) {
                        focusListener.onFocusChange(focusedView, false);
                    }
                } else {
                    // Ok right now focus is with listView. So set it back
                    // to the focusedView
                    // viewFocused = true;
                    focusedView.requestFocus();
                    focusedView.setOnFocusChangeListener(focusListener);
                    // Restore cursor position
                    if (cursorPosition != -1) {
                        ((EditText) focusedView).setSelection(cursorPosition);
                        selectionSet = true;
                    }

                }
            }
        }
        // @Override
        // protected void onLayout(boolean changed, int left, int top,
        // int right, int bottom) {
        //
        // super.onLayout(changed, left, top, right, bottom);
        // if (changed) {
        // TiUIHelper.firePostLayoutEvent(TiCollectionView.this);
        // }
        //
        // }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            if (mColumnsWidth != null && !mColumnsWidth.isUnitFixed()) {
                layoutManager.setColumnWidth(mColumnsWidth.getAsPixels(this));
                // } else if (gridAdapter.updateNumColumns()) {
                // adapter.notifyDataSetChanged();
            }
            layoutManager.requestColumnUpdate();
        }

        @Override
        public void dispatchSetPressed(boolean pressed) {
            if (propagateSetPressed(this, pressed)) {
                super.dispatchSetPressed(pressed);
            }
        }

        @Override
        public boolean dispatchTouchEvent(MotionEvent event) {
            if (touchPassThrough(getParentViewForChild(), event))
                return false;
            return super.dispatchTouchEvent(event);
        }

        // @Override
        // public boolean dispatchTouchEvent(MotionEvent event) {
        // if (touchPassThrough == true)
        // return false;
        // return super.dispatchTouchEvent(event);
        // }

        @Override
        protected void dispatchDraw(Canvas canvas) {
            try {
                super.dispatchDraw(canvas);
            } catch (IndexOutOfBoundsException e) {
                // samsung error
            }
        }
    };
    // mRecyclerView.setClipChildren(false);

    mAdapter = new TiBaseAdapter(mRecyclerView.getContext(), null);
    mAdapter.setDisplayHeaders(true);
    mSwipeMenuTouchListener = new SwipeMenuTouchListener(this);
    mRecyclerView.addOnItemTouchListener(mSwipeMenuTouchListener);
    mRecyclerView.setItemAnimator(new TiItemAnimator());
    mRecyclerView.setHorizontalScrollBarEnabled(false);
    mRecyclerView.setVerticalScrollBarEnabled(true);
    mRecyclerView.setHasFixedSize(true);
    layoutManager.setSmoothScrollbarEnabled(true);
    mRecyclerView.addOnScrollListener(new OnScrollListener() {
        private boolean scrollTouch = false;
        private Timer endTimer = null;

        public void cancelEndCall() {
            if (endTimer != null) {
                endTimer.cancel();
                endTimer = null;
            }
        }

        public void delayEndCall() {
            cancelEndCall();
            endTimer = new Timer();

            TimerTask action = new TimerTask() {
                public void run() {
                    scrollTouch = false;
                    if (fProxy.hasListeners(TiC.EVENT_SCROLLEND, false)) {
                        fProxy.fireEvent(TiC.EVENT_SCROLLEND, dictForScrollEvent(), false, false);
                    }
                }

            };

            this.endTimer.schedule(action, 200);
        }

        @Override
        public void onScrollStateChanged(RecyclerView view, int scrollState) {

            view.requestDisallowInterceptTouchEvent(scrollState != ViewPager.SCROLL_STATE_IDLE);
            if (scrollState == RecyclerView.SCROLL_STATE_IDLE) {
                if (scrollTouch) {
                    delayEndCall();
                }
            } else if (scrollState == RecyclerView.SCROLL_STATE_SETTLING) {
                cancelEndCall();
            } else if (scrollState == RecyclerView.SCROLL_STATE_DRAGGING) {
                cancelEndCall();
                if (hideKeyboardOnScroll && hasFocus()) {
                    blur();
                }
                if (scrollTouch == false) {
                    scrollTouch = true;
                    if (fProxy.hasListeners(TiC.EVENT_SCROLLSTART, false)) {
                        fProxy.fireEvent(TiC.EVENT_SCROLLSTART, dictForScrollEvent(), false, false);
                    }
                }
            }
        }

        @Override
        public void onScrolled(RecyclerView view, int dx, int dy) {
            if (dx == 0 && dy == 0) {
                return;
            }

            if (fProxy.hasListeners(TiC.EVENT_SCROLL, false)) {
                fProxy.fireEvent(TiC.EVENT_SCROLL, dictForScrollEvent(), false, false);
            }
        }
    });

    // mRecyclerView.setOnStickyHeaderChangedListener(
    // new OnStickyHeaderChangedListener() {
    //
    // @Override
    // public void onStickyHeaderChanged(
    // StickyListHeadersListViewAbstract l, View header,
    // int itemPosition, long headerId) {
    // // for us headerId is the section index
    // int sectionIndex = (int) headerId;
    // if (fProxy.hasListeners(TiC.EVENT_HEADER_CHANGE,
    // false)) {
    // KrollDict data = new KrollDict();
    // AbsListSectionProxy section = null;
    // synchronized (sections) {
    // if (sectionIndex >= 0
    // && sectionIndex < sections.size()) {
    // section = sections.get(sectionIndex);
    // } else {
    // return;
    // }
    // }
    // data.put(TiC.PROPERTY_HEADER_VIEW, section
    // .getHoldedProxy(TiC.PROPERTY_HEADER_VIEW));
    // data.put(TiC.PROPERTY_SECTION, section);
    // data.put(TiC.PROPERTY_SECTION_INDEX, sectionIndex);
    // fProxy.fireEvent(TiC.EVENT_HEADER_CHANGE, data,
    // false, false);
    // }
    // }
    // });

    mRecyclerView.setEnabled(true);
    getLayoutParams().autoFillsHeight = true;
    getLayoutParams().autoFillsWidth = true;
    mRecyclerView.setFocusable(true);
    mRecyclerView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);

    // try {
    //     isCheck = TiRHelper.getApplicationResource(
    //             "drawable.btn_check_buttonless_on_64");
    //     hasChild = TiRHelper.getApplicationResource("drawable.btn_more_64");
    //     disclosure = TiRHelper
    //             .getApplicationResource("drawable.disclosure_64");
    // } catch (ResourceNotFoundException e) {
    //     Log.e(TAG, "XML resources could not be found!!!", Log.DEBUG_MODE);
    // }

    // RelativeLayout layout = new RelativeLayout(proxy.getActivity()) {
    // @Override
    // protected void onLayout(boolean changed, int left, int top,
    // int right, int bottom) {
    // super.onLayout(changed, left, top, right, bottom);
    // if (changed) {
    // TiUIHelper.firePostLayoutEvent(TiCollectionView.this);
    // }
    // }
    //
    // @Override
    // public void dispatchSetPressed(boolean pressed) {
    // if (propagateSetPressed(this, pressed)) {
    // super.dispatchSetPressed(pressed);
    // }
    // }
    //
    // @Override
    // public boolean dispatchTouchEvent(MotionEvent event) {
    // if (touchPassThrough(getParentViewForChild(), event))
    // return false;
    // return super.dispatchTouchEvent(event);
    // }
    //
    // };
    // layout.addView(mRecyclerView);
    setNativeView(mRecyclerView);

    // needs to be fired after because
    // getStickyHeadersHolder will be called and need nativeView
    mRecyclerView.setLayoutManager(layoutManager);
    mAdapter.setLayoutManager(layoutManager);
    mRecyclerView.setAdapter(mAdapter);
}