태그 보관물: programmatically-created

programmatically-created

프로그래밍 방식으로 스크롤보기를 특정 편집 텍스트로 스크롤하는 방법이 있습니까? 해야하는 다양한 필드가있는 양식입니다. 양식의 절반 아래에

스크롤보기로 매우 긴 활동이 있습니다. 사용자가 작성 해야하는 다양한 필드가있는 양식입니다. 양식의 절반 아래에 확인란이 있으며 사용자가 확인하면보기의 특정 부분으로 스크롤하고 싶습니다. 프로그래밍 방식으로 EditText 객체 (또는 다른 뷰 객체)로 스크롤하는 방법이 있습니까?

또한 X 및 Y 좌표를 사용하여 이것이 가능하다는 것을 알고 있지만 양식이 사용자마다 변경 될 수 있으므로이 작업을 피하고 싶습니다.



답변

private final void focusOnView(){
        your_scrollview.post(new Runnable() {
            @Override
            public void run() {
                your_scrollview.scrollTo(0, your_EditBox.getBottom());
            }
        });
    }


답변

보기를 스크롤보기의 중앙으로 스크롤 하려는 경우 Sherif elKhatib의 대답을 크게 향상시킬 수 있습니다 . 이 재사용 가능한 메소드는 뷰를 HorizontalScrollView의 보이는 중심으로 부드럽게 스크롤합니다.

private final void focusOnView(final HorizontalScrollView scroll, final View view) {
    new Handler().post(new Runnable() {
        @Override
        public void run() {
            int vLeft = view.getLeft();
            int vRight = view.getRight();
            int sWidth = scroll.getWidth();
            scroll.smoothScrollTo(((vLeft + vRight - sWidth) / 2), 0);
        }
    });
}

수직 ScrollView사용

...
int vTop = view.getTop();
int vBottom = view.getBottom();
int sHeight = scroll.getBottom();
scroll.smoothScrollTo(((vTop + vBottom - sHeight) / 2), 0);
...


답변

이것은 나를 위해 잘 작동합니다 :

  targetView.getParent().requestChildFocus(targetView,targetView);

public void RequestChildFocus (자식보기, 초점보기)

아이 – 초점을 원하는이 ViewParent의 아이입니다. 이 뷰에는 포커스 된 뷰가 포함됩니다. 실제로 포커스가있는 것은 아닙니다.

집중 -실제로 초점이있는 자녀의 후손


답변

내 의견으로는 주어진 사각형으로 스크롤하는 가장 좋은 방법은 View.requestRectangleOnScreen(Rect, Boolean)입니다. View스크롤하려는 화면에서 호출 하고 화면에 표시하려는 로컬 사각형을 전달해야합니다. 두 번째 매개 변수는 false부드러운 스크롤 및 true즉각적인 스크롤을위한 것이어야합니다 .

final Rect rect = new Rect(0, 0, view.getWidth(), view.getHeight());
view.requestRectangleOnScreen(rect, false);


답변

나는 WarrenFaith의 응답을 기반으로 작은 유틸리티 방법을 만들었습니다.이 코드는 스크롤보기에서 해당보기가 이미 표시되어 있는지 고려합니다.

public static void scrollToView(final ScrollView scrollView, final View view) {

    // View needs a focus
    view.requestFocus();

    // Determine if scroll needs to happen
    final Rect scrollBounds = new Rect();
    scrollView.getHitRect(scrollBounds);
    if (!view.getLocalVisibleRect(scrollBounds)) {
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                scrollView.smoothScrollTo(0, view.getBottom());
            }
        });
    }
}


답변

TextView요청에 집중 해야합니다 .

    mTextView.requestFocus();


답변

내 EditText는 내 ScrollView 내부에 여러 레이어로 중첩되어 있으며 레이아웃 자체는 루트 뷰가 아닙니다. getTop ()과 getBottom ()은 뷰에 포함 된 좌표를보고하는 것처럼 보였으므로 EditText의 부모를 반복하여 ScrollView의 상단에서 EditText의 상단까지의 거리를 계산했습니다.

// Scroll the view so that the touched editText is near the top of the scroll view
new Thread(new Runnable()
{
    @Override
    public
    void run ()
    {
        // Make it feel like a two step process
        Utils.sleep(333);

        // Determine where to set the scroll-to to by measuring the distance from the top of the scroll view
        // to the control to focus on by summing the "top" position of each view in the hierarchy.
        int yDistanceToControlsView = 0;
        View parentView = (View) m_editTextControl.getParent();
        while (true)
        {
            if (parentView.equals(scrollView))
            {
                break;
            }
            yDistanceToControlsView += parentView.getTop();
            parentView = (View) parentView.getParent();
        }

        // Compute the final position value for the top and bottom of the control in the scroll view.
        final int topInScrollView = yDistanceToControlsView + m_editTextControl.getTop();
        final int bottomInScrollView = yDistanceToControlsView + m_editTextControl.getBottom();

        // Post the scroll action to happen on the scrollView with the UI thread.
        scrollView.post(new Runnable()
        {
            @Override
            public void run()
            {
                int height =m_editTextControl.getHeight();
                scrollView.smoothScrollTo(0, ((topInScrollView + bottomInScrollView) / 2) - height);
                m_editTextControl.requestFocus();
            }
        });
    }
}).start();