How to scroll to bottom in a ScrollView on activity startup

后端 未结 9 989
Happy的楠姐
Happy的楠姐 2020-11-27 14:38

I am displaying some data in a ScrollView. On activity startup (method onCreate) I fill the ScrollView with data and want to scroll to the bottom.

I tried to use

相关标签:
9条回答
  • 2020-11-27 15:10

    Try this

        final ScrollView scrollview = ((ScrollView) findViewById(R.id.scrollview));
        scrollview.post(new Runnable() {
           @Override
           public void run() {
             scrollview.fullScroll(ScrollView.FOCUS_DOWN);
           }
        });
    
    0 讨论(0)
  • 2020-11-27 15:14

    This works for me:

    scrollview.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            scrollview.post(new Runnable() {
                public void run() {
                    scrollview.fullScroll(View.FOCUS_DOWN);
                }
            });
        }
    });
    
    0 讨论(0)
  • 2020-11-27 15:19

    Right after you append data to the view add this single line:

    yourScrollview.fullScroll(ScrollView.FOCUS_DOWN);
    
    0 讨论(0)
  • 2020-11-27 15:20

    This is the best way of doing this.

    scrollView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                scrollView.post(new Runnable() {
                    @Override
                    public void run() {
                        scrollView.fullScroll(View.FOCUS_DOWN);
                    }
                });
            }
    });
    
    0 讨论(0)
  • 2020-11-27 15:25

    After initializing your UI component and fill it with data. add those line to your on create method

    Runnable runnable=new Runnable() {
            @Override
            public void run() {
                scrollView.fullScroll(ScrollView.FOCUS_DOWN);
            }
        };
        scrollView.post(runnable);
    
    0 讨论(0)
  • You can do this in layout file:

                    android:id="@+id/listViewContent"
                    android:layout_width="wrap_content"
                    android:layout_height="381dp" 
                    android:stackFromBottom="true"
                    android:transcriptMode="alwaysScroll">
    
    0 讨论(0)
提交回复
热议问题