Java Code Examples for android.view.View#requestFocus()

The following examples show how to use android.view.View#requestFocus() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GridLayoutManager.java    From TvRecyclerView with Apache License 2.0 6 votes vote down vote up
/**
 * In layout, if there is a view that needs to get focus,
 * make sure the view is displayed.
 */
private void scrollToFocusViewInLayout() {
    if (hasFocus() && !isSmoothScrolling() && mFocusPosition != NO_POSITION) {
        View focusView = findViewByPosition(mFocusPosition);
        if (focusView != null) {
            if (getScrollPosition(focusView, mTwoInts)) {
                int maxScrollDistance = getMaxScrollDistance();
                int scrollDistance = mTwoInts[0];
                if (scrollDistance > 0) {
                    if (mHorizontalOffset + scrollDistance > maxScrollDistance) {
                        scrollDistance = maxScrollDistance - mHorizontalOffset;
                    }
                    offsetChildrenPrimary(-scrollDistance);
                }
                if (!focusView.hasFocus()) {
                    focusView.requestFocus();
                    dispatchChildSelected();
                }
            }
        } else if (mFocusPosition != NO_POSITION){
            scrollToSelection(mBaseRecyclerView, mFocusPosition, mSubFocusPosition, true,
                    mPrimaryScrollExtra);
        }
    }
}
 
Example 2
Source File: LightningView.java    From JumpGo with Mozilla Public License 2.0 6 votes vote down vote up
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(@Nullable View view, @NonNull MotionEvent arg1) {
    if (view == null)
        return false;

    if (!view.hasFocus()) {
        view.requestFocus();
    }
    mAction = arg1.getAction();
    mY = arg1.getY();
    if (mAction == MotionEvent.ACTION_DOWN) {
        mLocation = mY;
    } else if (mAction == MotionEvent.ACTION_UP) {
        final float distance = (mY - mLocation);
        if (distance > SCROLL_UP_THRESHOLD && view.getScrollY() < SCROLL_UP_THRESHOLD) {
            mUIController.showActionBar();
        } else if (distance < -SCROLL_UP_THRESHOLD) {
            mUIController.hideActionBar();
        }
        mLocation = 0;
    }
    mGestureDetector.onTouchEvent(arg1);
    return false;
}
 
Example 3
Source File: TabContentViewParent.java    From AndroidChromium with Apache License 2.0 6 votes vote down vote up
@Override
public void onContentChanged(Tab tab) {
    // If the tab is frozen, both native page and content view are not ready.
    if (tab.isFrozen()) return;

    View viewToShow = getViewToShow(tab);
    if (isShowing(viewToShow)) return;

    removeCurrentContent();
    LayoutParams lp = (LayoutParams) viewToShow.getLayoutParams();
    if (lp == null) {
        lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    }
    // Weirdly enough, if gravity is not top, top_margin is not respected by FrameLayout.
    // Yet for many native pages on tablet, top_margin is necessary to not overlap the tab
    // switcher.
    lp.gravity = Gravity.TOP;
    UiUtils.removeViewFromParent(viewToShow);
    addView(viewToShow, CONTENT_INDEX, lp);
    viewToShow.requestFocus();
}
 
Example 4
Source File: BaseFragmentActivity.java    From umeng_community_android with MIT License 6 votes vote down vote up
public void handleMessage(android.os.Message msg) {
    View view = (View) msg.obj;
    // 显示软键盘
    if (msg.what == Constants.INPUT_METHOD_SHOW) {
        boolean result = mInputMan.showSoftInput(view, 0);
        if (!result && totalTime < Constants.LIMIT_TIME) {
            totalTime += Constants.IDLE;
            Message message = Message.obtain(msg);
            mHandler.sendMessageDelayed(message, Constants.IDLE);
        } else if (!isFinish) {
            totalTime = 0;
            result = view.requestFocus();
            isFinish = true;
        }
    } else if (msg.what == Constants.INPUT_METHOD_DISAPPEAR) {
        // 隐藏软键盘
        mInputMan.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

}
 
Example 5
Source File: DpadAwareRecyclerView.java    From dpad-aware-recycler-view with Apache License 2.0 6 votes vote down vote up
@Override
protected void onFocusChanged(boolean gainFocus, int direction, @Nullable Rect previouslyFocusedRect) {
    super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);

    if (gainFocus) {
        // We favor natural focus if we don't want to remember focus AND if previously focused
        // rectangle is NOT null. Usually latter condition holds true if simple requestFocus()
        // is called and holds false if focus changed during navigation. Overall this is done
        // because of backward compatibility - some code is dependent on the fact that
        // focused position will be restored if requestFocus() is called and it expects
        // to have natural focus when ordinary navigation happens.
        boolean favorNaturalFocus = !mRememberLastFocus && previouslyFocusedRect != null;
        View lastFocusedView = mFocusArchivist.getLastFocus(this);
        if (favorNaturalFocus || lastFocusedView == null) {
            requestNaturalFocus(direction, previouslyFocusedRect);
        } else {
            lastFocusedView.requestFocus();
        }
    }
}
 
Example 6
Source File: AlertPopup.java    From arcusandroid with Apache License 2.0 6 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = super.onCreateView(inflater, container, savedInstanceState);

    closeBtn.setOnClickListener(this);

    view.setFocusableInTouchMode(true);
    view.requestFocus();
    view.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
                doClose();
                return true;
            } else {
                return false;
            }
        }
    });

    return view;
}
 
Example 7
Source File: ViewPager.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
/**
 * We only want the current page that is being shown to be focusable.
 */
@Override
protected boolean onRequestFocusInDescendants(int direction,
        Rect previouslyFocusedRect) {
    int index;
    int increment;
    int end;
    int count = getChildCount();
    if ((direction & FOCUS_FORWARD) != 0) {
        index = 0;
        increment = 1;
        end = count;
    } else {
        index = count - 1;
        increment = -1;
        end = -1;
    }
    for (int i = index; i != end; i += increment) {
        View child = getChildAt(i);
        if (child.getVisibility() == VISIBLE) {
            ItemInfo ii = infoForChild(child);
            if (ii != null && ii.position == mCurItem) {
                if (child.requestFocus(direction, previouslyFocusedRect)) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 8
Source File: Workspace.java    From TurboLauncher with Apache License 2.0 5 votes vote down vote up
void moveToCustomContentScreen(boolean animate) {
    if (hasCustomContent()) {
        int ccIndex = getPageIndexForScreenId(CUSTOM_CONTENT_SCREEN_ID);
        if (animate) {
            snapToPage(ccIndex);
        } else {
            setCurrentPage(ccIndex);
        }
        View child = getChildAt(ccIndex);
        if (child != null) {
            child.requestFocus();
        }
     }
    exitWidgetResizeMode();
}
 
Example 9
Source File: CustomScrollView.java    From zhangshangwuda with Apache License 2.0 5 votes vote down vote up
private boolean scrollAndFocusH(int direction, int left, int right) {
	boolean handled = true;

	int width = getWidth();
	int containerLeft = getScrollX();
	int containerRight = containerLeft + width;
	boolean goLeft = direction == View.FOCUS_LEFT;

	View newFocused = findFocusableViewInBoundsH(goLeft, left, right);
	if (newFocused == null) {
		newFocused = this;
	}

	if (left >= containerLeft && right <= containerRight) {
		handled = false;
	} else {
		int delta = goLeft ? (left - containerLeft)
				: (right - containerRight);
		doScrollX(delta);
	}

	if (newFocused != findFocus() && newFocused.requestFocus(direction)) {
		mScrollViewMovedFocus = true;
		mScrollViewMovedFocus = false;
	}

	return handled;
}
 
Example 10
Source File: ViewPager.java    From guideshow with MIT License 5 votes vote down vote up
/**
 * We only want the current page that is being shown to be focusable.
 */
@Override
protected boolean onRequestFocusInDescendants(int direction,
        Rect previouslyFocusedRect) {
    int index;
    int increment;
    int end;
    int count = getChildCount();
    if ((direction & FOCUS_FORWARD) != 0) {
        index = 0;
        increment = 1;
        end = count;
    } else {
        index = count - 1;
        increment = -1;
        end = -1;
    }
    for (int i = index; i != end; i += increment) {
        View child = getChildAt(i);
        if (child.getVisibility() == VISIBLE) {
            ItemInfo ii = infoForChild(child);
            if (ii != null && ii.position == mCurItem) {
                if (child.requestFocus(direction, previouslyFocusedRect)) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 11
Source File: TwoDScrollView.java    From AndroidTreeView with Apache License 2.0 5 votes vote down vote up
/**
 * Fling the scroll view
 *
 * @param velocityY The initial velocity in the Y direction. Positive
 *                  numbers mean that the finger/curor is moving down the screen,
 *                  which means we want to scroll towards the top.
 */
public void fling(int velocityX, int velocityY) {
    if (getChildCount() > 0) {
        int height = getHeight() - getPaddingBottom() - getPaddingTop();
        int bottom = getChildAt(0).getHeight();
        int width = getWidth() - getPaddingRight() - getPaddingLeft();
        int right = getChildAt(0).getWidth();

        mScroller.fling(getScrollX(), getScrollY(), velocityX, velocityY, 0, right - width, 0, bottom - height);

        final boolean movingDown = velocityY > 0;
        final boolean movingRight = velocityX > 0;

        View newFocused = findFocusableViewInMyBounds(movingRight, mScroller.getFinalX(), movingDown, mScroller.getFinalY(), findFocus());
        if (newFocused == null) {
            newFocused = this;
        }

        if (newFocused != findFocus() && newFocused.requestFocus(movingDown ? View.FOCUS_DOWN : View.FOCUS_UP)) {
            mTwoDScrollViewMovedFocus = true;
            mTwoDScrollViewMovedFocus = false;
        }

        awakenScrollBars(mScroller.getDuration());
        invalidate();
    }
}
 
Example 12
Source File: BasicActivity.java    From letv with Apache License 2.0 5 votes vote down vote up
public void setViewFocus(View view) {
    if (view != null) {
        view.setFocusable(true);
        view.setFocusableInTouchMode(true);
        view.requestFocus();
        view.requestFocusFromTouch();
    }
}
 
Example 13
Source File: DpadAwareRecyclerView.java    From dpad-aware-recycler-view with Apache License 2.0 5 votes vote down vote up
/**
 * Request natural focus.
 *
 * @param direction             direction in which focus is changing
 * @param previouslyFocusedRect previously focus rectangle
 */
private void requestNaturalFocus(int direction, Rect previouslyFocusedRect) {
    FocusFinder ff = FocusFinder.getInstance();
    previouslyFocusedRect = previouslyFocusedRect == null
            ? new Rect(0, 0, 0, 0) : previouslyFocusedRect;
    View toFocus = ff.findNextFocusFromRect(this, previouslyFocusedRect, direction);
    toFocus = toFocus == null ? getChildAt(0) : toFocus;
    if (toFocus != null) {
        toFocus.requestFocus();
    }
}
 
Example 14
Source File: VerticalViewPager.java    From DKVideoPlayer with Apache License 2.0 5 votes vote down vote up
/**
 * We only want the current page that is being shown to be focusable.
 */
@Override
protected boolean onRequestFocusInDescendants(int direction,
                                              Rect previouslyFocusedRect) {
    int index;
    int increment;
    int end;
    int count = getChildCount();
    if ((direction & FOCUS_FORWARD) != 0) {
        index = 0;
        increment = 1;
        end = count;
    } else {
        index = count - 1;
        increment = -1;
        end = -1;
    }
    for (int i = index; i != end; i += increment) {
        View child = getChildAt(i);
        if (child.getVisibility() == VISIBLE) {
            ItemInfo ii = infoForChild(child);
            if (ii != null && ii.position == mCurItem) {
                if (child.requestFocus(direction, previouslyFocusedRect)) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 15
Source File: CustomViewAbove.java    From appcan-android with GNU Lesser General Public License v3.0 5 votes vote down vote up
public boolean arrowScroll(int direction) {
    View currentFocused = findFocus();
    if (currentFocused == this) currentFocused = null;

    boolean handled = false;

    View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused,
            direction);
    if (nextFocused != null && nextFocused != currentFocused) {
        if (direction == View.FOCUS_LEFT) {
            handled = nextFocused.requestFocus();
        } else if (direction == View.FOCUS_RIGHT) {
            // If there is nothing to the right, or this is causing us to
            // jump to the left, then what we really want to do is page right.
            if (currentFocused != null && nextFocused.getLeft() <= currentFocused.getLeft()) {
                handled = pageRight();
            } else {
                handled = nextFocused.requestFocus();
            }
        }
    } else if (direction == FOCUS_LEFT || direction == FOCUS_BACKWARD) {
        // Trying to move left and nothing there; try to page.
        handled = pageLeft();
    } else if (direction == FOCUS_RIGHT || direction == FOCUS_FORWARD) {
        // Trying to move right and nothing there; try to page.
        handled = pageRight();
    }
    if (handled) {
        playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
    }
    return handled;
}
 
Example 16
Source File: DragLayer.java    From LaunchEnr with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
    View topView = AbstractFloatingView.getTopOpenView(mLauncher);
    if (topView != null) {
        return topView.requestFocus(direction, previouslyFocusedRect);
    } else {
        return super.onRequestFocusInDescendants(direction, previouslyFocusedRect);
    }
}
 
Example 17
Source File: FocusBinding.java    From android-utils with MIT License 5 votes vote down vote up
@Override
public void run(Boolean focus, View view) {
	if (view == null)
		return;
	if (focus) {
		view.setFocusable(true);
		view.requestFocus();
	}
	else {
		view.clearFocus();
		view.setFocusable(false);
	}
}
 
Example 18
Source File: VectorClippedEventDelegate.java    From FirefoxReality with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public boolean onTouch(View v, MotionEvent event) {
    v.getParent().requestDisallowInterceptTouchEvent(true);

    if(!isInside(event)) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_UP:
                if (mTouched) {
                    v.requestFocus();
                    v.requestFocusFromTouch();
                    if (mClickListener != null) {
                        mClickListener.onClick(v);
                    }
                }
                v.setPressed(false);
                mTouched = false;
        }

        return true;

    } else {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                v.setPressed(true);
                mTouched = true;
                return true;

            case MotionEvent.ACTION_UP:
                if (mTouched && ViewUtils.isInsideView(v, (int)event.getRawX(), (int)event.getRawY())) {
                    v.requestFocus();
                    v.requestFocusFromTouch();
                    if (mClickListener != null) {
                        mClickListener.onClick(v);
                    }
                }
                v.setPressed(false);
                mTouched = false;
                return true;

            case MotionEvent.ACTION_MOVE:
                return true;

            case MotionEvent.ACTION_CANCEL:
                v.setPressed(false);
                mTouched = false;
                return true;
        }

        return false;
    }
}
 
Example 19
Source File: PipService.java    From zidoorecorder with Apache License 2.0 4 votes vote down vote up
/**
 * 初始化控件
 */
private void initView()
{
	mVPip = LayoutInflater.from(getApplicationContext()).inflate(R.layout.view_pip, null);
	View rlPip = (RelativeLayout) mVPip.findViewById(R.id.rl_pip);
	mFmPip = (FrameLayout) rlPip.findViewById(R.id.fm_pip);
	mLnFrameBg = (LinearLayout) rlPip.findViewById(R.id.ln_bg);
	mPgbLoading = (ProgressBar) mFmPip.findViewById(R.id.pgb_loading);
	mRlFrame = (RelativeLayout) mFmPip.findViewById(R.id.rl_frame);
	mTvNoSingle = (TextView) mFmPip.findViewById(R.id.tv_no_single);

	mRlController = (RelativeLayout) mVPip.findViewById(R.id.rl_control);
	mLnController = (LinearLayout) mRlController.findViewById(R.id.ln_controller);
	mLnScale = (LinearLayout) mRlController.findViewById(R.id.ln_scale);
	mLnAudio = (LinearLayout) mRlController.findViewById(R.id.ln_audio);
	mImgSwitch = (ImageView) mLnAudio.findViewById(R.id.img_switch);
	mTvReminds = (TextView) mRlController.findViewById(R.id.tv_reminds);

	mSfPip = (SurfaceView) mFmPip.findViewById(R.id.sf_pip);
	SurfaceHolder holder = mSfPip.getHolder();
	holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	holder.addCallback(this);

	View move = mLnController.findViewById(R.id.tv_move);
	mTvScale = (TextView) mLnController.findViewById(R.id.tv_scale);
	mTvAudio = (TextView) mLnController.findViewById(R.id.tv_audio);
	View exit = mLnController.findViewById(R.id.tv_exit);
	View audioSwitch = mLnAudio.findViewById(R.id.rl_audio);

	mScales = new TextView[4];
	mScales[0] = mLnScale.findViewById(R.id.tv_scale_0);
	mScales[1] = mLnScale.findViewById(R.id.tv_scale_1);
	mScales[2] = mLnScale.findViewById(R.id.tv_scale_2);
	mScales[3] = mLnScale.findViewById(R.id.tv_scale_3);

	for (int i = 0; i < mScales.length; i++)
	{
		View v = mScales[i];
		v.setTag(i);
		v.setOnClickListener(this);
		v.setOnKeyListener(this);
		v.setOnFocusChangeListener(this);
	}
	mScales[mScaleMode].setSelected(true);

	move.setOnClickListener(this);
	mTvScale.setOnClickListener(this);
	mTvAudio.setOnClickListener(this);
	exit.setOnClickListener(this);
	audioSwitch.setOnClickListener(this);

	move.setOnFocusChangeListener(this);
	mTvScale.setOnFocusChangeListener(this);
	mTvAudio.setOnFocusChangeListener(this);
	exit.setOnFocusChangeListener(this);
	audioSwitch.setOnFocusChangeListener(this);

	move.setOnKeyListener(this);
	mTvScale.setOnKeyListener(this);
	mTvAudio.setOnKeyListener(this);
	exit.setOnKeyListener(this);
	audioSwitch.setOnKeyListener(this);
	mFmPip.setOnKeyListener(this);

	adjustPipFrame(mWindowType.x, mWindowType.y, mWindowType.width, mWindowType.height);

	WindowManager.LayoutParams params = new WindowManager.LayoutParams();
	params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
	params.format = PixelFormat.RGBA_8888; // 设置图片格式,效果为背景透明
	params.flags = WindowManager.LayoutParams.FLAG_FULLSCREEN;
	params.width = WindowManager.LayoutParams.MATCH_PARENT;
	params.height = WindowManager.LayoutParams.MATCH_PARENT;

	((WindowManager) getSystemService(WINDOW_SERVICE)).addView(mVPip, params);
	move.requestFocus();
}
 
Example 20
Source File: RegisterActivity.java    From SnapchatClone with MIT License 4 votes vote down vote up
private void requestFocus(View view) {
    if (view.requestFocus()) {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }
}