How to scroll to top of long ScrollView layout?

前端 未结 15 783
轻奢々
轻奢々 2020-11-30 18:34

For part of my app, the user is presented with a list of names and is asked to group them as they see fit.

(Note, the ListView code was copied verbatim from the And

相关标签:
15条回答
  • 2020-11-30 18:47
    scrollView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            // Ready, move up
            scrollView.fullScroll(View.FOCUS_UP);
        }
    });
    
    0 讨论(0)
  • 2020-11-30 18:49

    The main problem to all these valid solutions is that you are trying to move up the scroll when it isn't drawn on screen yet.

    You need to wait until scroll view is on screen, then move it to up with any of these solutions. This is better solution than a postdelay, because you don't mind about the delay.

        // Wait until my scrollView is ready
        scrollView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                // Ready, move up
                scrollView.fullScroll(View.FOCUS_UP);
            }
        });
    
    0 讨论(0)
  • 2020-11-30 18:51

    This is force scroll for scrollview :

                scrollView.post(new Runnable() {
                    @Override
                    public void run() {
                        scrollView.scrollTo(0, 0);
                        scrollView.pageScroll(View.FOCUS_UP);
                        scrollView.smoothScrollTo(0,0);
                    }
                });
    
    0 讨论(0)
  • 2020-11-30 18:52

    Very easy

        ScrollView scroll = (ScrollView) findViewById(R.id.addresses_scroll);
        scroll.setFocusableInTouchMode(true);
        scroll.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
    
    0 讨论(0)
  • 2020-11-30 18:53

    Try

    mainScrollView.fullScroll(ScrollView.FOCUS_UP);
    

    it should work.

    0 讨论(0)
  • 2020-11-30 18:55
    runOnUiThread( new Runnable(){
       @Override
       public void run(){
          mainScrollView.fullScroll(ScrollView.FOCUS_UP);
       }
    }
    
    0 讨论(0)
提交回复
热议问题