Android : ScrollView 강제로 맨 아래 싶습니다. 어떤 방법이

ScrollView를 맨 처음부터 시작하고 싶습니다. 어떤 방법이 있습니까?



답변

scroll.fullScroll(View.FOCUS_DOWN) 또한 작동해야합니다.

이것을 넣어 scroll.Post(Runnable run)

코 틀린 코드

scrollView.post {
   scrollView.fullScroll(View.FOCUS_DOWN)
}

답변

다음과 같이 scroll.post 내부에서 코드를 실행해야합니다.

scroll.post(new Runnable() {
    @Override
    public void run() {
           scroll.fullScroll(View.FOCUS_DOWN);
    }
});

답변

scroll.fullScroll(View.FOCUS_DOWN)초점의 변화로 이어질 것입니다. 두 개의 EditText와 같이 둘 이상의 포커스 가능한 뷰가있는 경우 이상한 동작이 발생합니다. 이 질문에는 다른 방법이 있습니다.

    View lastChild = scrollLayout.getChildAt(scrollLayout.getChildCount() - 1);
    int bottom = lastChild.getBottom() + scrollLayout.getPaddingBottom();
    int sy = scrollLayout.getScrollY();
    int sh = scrollLayout.getHeight();
    int delta = bottom - (sy + sh);

    scrollLayout.smoothScrollBy(0, delta);

이것은 잘 작동합니다.

코 틀린 확장

fun ScrollView.scrollToBottom() {
    val lastChild = getChildAt(childCount - 1)
    val bottom = lastChild.bottom + paddingBottom
    val delta = bottom - (scrollY+ height)
    smoothScrollBy(0, delta)
}

답변

때때로 scrollView.post가 작동하지 않습니다

 scrollView.post(new Runnable() {
        @Override
        public void run() {
            scrollView.fullScroll(ScrollView.FOCUS_DOWN);
        }
    });

그러나 scrollView.postDelayed를 사용하면 확실히 작동합니다.

 scrollView.postDelayed(new Runnable() {
        @Override
        public void run() {
            scrollView.fullScroll(ScrollView.FOCUS_DOWN);
        }
    },1000);

답변

나에게 가장 효과가 있었던 것은

scroll_view.post(new Runnable() {
     @Override
     public void run() {
         // This method works but animates the scrolling 
         // which looks weird on first load
         // scroll_view.fullScroll(View.FOCUS_DOWN);

         // This method works even better because there are no animations.
         scroll_view.scrollTo(0, scroll_view.getBottom());
     }
});

답변

나는 완벽하게 작동하도록 증분합니다.

    private void sendScroll(){
        final Handler handler = new Handler();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {Thread.sleep(100);} catch (InterruptedException e) {}
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        scrollView.fullScroll(View.FOCUS_DOWN);
                    }
                });
            }
        }).start();
    }

노트

이 답변은 이전 버전의 Android에 대한 해결 방법입니다. 오늘날 postDelayed에는 더 이상 버그가 없으므로 사용해야합니다.


답변

나는 그것을 성공적으로 시도했다.

scrollView.postDelayed(new Runnable() {
    @Override
    public void run() {
        scrollView.smoothScrollTo(0, scrollView.getHeight());
    }
}, 1000);