Java 我可以在 Android 中以编程方式滚动 ScrollView 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6438061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:55:23  来源:igfitidea点击:

Can I scroll a ScrollView programmatically in Android?

javaandroidscrollviewandroid-scrollview

提问by Arun Badole

Is there any way to scroll a ScrollViewprogrammatically to a certain position?

有没有办法以ScrollView编程方式滚动到某个位置?

I have created dynamic TableLayoutwhich is placed in a ScrollView. So I want that on a specific action (like clicking a Button, etc.) the particular row should scroll automatically to a top position.

我创建了TableLayout放置在ScrollView. 因此,我希望在特定操作(例如单击按钮等)上,特定行应自动滚动到顶部位置。

Is it possible?

是否可以?

采纳答案by Android

ScrollView sv = (ScrollView)findViewById(R.id.scrl);
sv.scrollTo(0, sv.getBottom());

or

或者

sv.scrollTo(5, 10);

sv.scrollTo(5, 10);

回答by bluefalcon

Try using scrollTomethod More Info

尝试使用scrollTo方法更多信息

回答by Srichand Yella

Use something like this:

使用这样的东西:

mScrollView.scrollBy(10, 10);

or

或者

mScrollView.scrollTo(10, 10);

回答by ercu

The answer from Pragna does not work always, try this:

Pragna 的答案并不总是有效,试试这个:

mScrollView.post(new Runnable() { 
        public void run() { 
             mScrollView.scrollTo(0, mScrollView.getBottom());
        } 
});

or

或者

mScrollView.post(new Runnable() { 
        public void run() { 
             mScrollView.fullScroll(mScrollView.FOCUS_DOWN);
        } 
});

if You want to scroll to start

如果你想滚动开始

mScrollView.post(new Runnable() { 
        public void run() { 
             mScrollView.fullScroll(mScrollView.FOCUS_UP);
        } 
});

回答by Patrick Favre

I wanted the scrollView to scroll directly after onCreateView() (not after e.g. a button click). To get it to work I needed to use a ViewTreeObserver:

我希望 scrollView 在 onCreateView() 之后直接滚动(而不是在单击按钮之后)。为了让它工作,我需要使用 ViewTreeObserver:

mScrollView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            mScrollView.post(new Runnable() {
                public void run() {
                    mScrollView.fullScroll(View.FOCUS_DOWN);
                }
            });
        }
    });

But beware that this will be called everytime something gets layouted (e.g if you set a view invisible or similar) so don't forget to remove this listener if you don't need it anymore with:

但请注意,每次布局时都会调用它(例如,如果您将视图设置为不可见或类似),因此如果您不再需要它,请不要忘记删除此侦听器:

public void removeGlobalOnLayoutListener (ViewTreeObserver.OnGlobalLayoutListener victim)on SDK Lvl < 16

public void removeGlobalOnLayoutListener (ViewTreeObserver.OnGlobalLayoutListener victim)SDK Lvl < 16

or

或者

public void removeOnGlobalLayoutListener (ViewTreeObserver.OnGlobalLayoutListener victim)in SDK Lvl >= 16

public void removeOnGlobalLayoutListener (ViewTreeObserver.OnGlobalLayoutListener victim)在 SDK 等级 >= 16

回答by djdance

Note: if you already in a thread, you have to make a new post thread, or it's not scroll new long height till the full end (for me). For ex:

注意:如果你已经在一个线程中,你必须创建一个新的帖子线程,否则它不会滚动到新的长高度(对我来说)。例如:

void LogMe(final String s){
    runOnUiThread(new Runnable() {
        public void run() {
            connectionLog.setText(connectionLog.getText() + "\n" + s);
            final ScrollView sv = (ScrollView)connectLayout.findViewById(R.id.scrollView);
            sv.post(new Runnable() {
                public void run() {
                    sv.fullScroll(sv.FOCUS_DOWN);
                    /*
                    sv.scrollTo(0,sv.getBottom());
                    sv.scrollBy(0,sv.getHeight());*/
                }
            });
        }
    });
}

回答by Maddy

If you want to scroll instantly then you can use :

如果您想立即滚动,则可以使用:

ScrollView scroll= (ScrollView)findViewById(R.id.scroll);
scroll.scrollTo(0, scroll.getBottom());

            OR

scroll.fullScroll(View.FOCUS_DOWN);

            OR

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

Or if you want to scroll smoothly and slowly so you can use this:

或者,如果您想平稳缓慢地滚动,则可以使用以下命令:

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();
    }

回答by Sebasu

I was using the Runnable with sv.fullScroll(View.FOCUS_DOWN); It works perfectly for the immediate problem, but that method makes ScrollView take the Focus from the entire screen, if you make that AutoScroll to happen every time, no EditText will be able to receive information from the user, my solution was use a different code under the runnable:

我正在使用 Runnable 和 sv.fullScroll(View.FOCUS_DOWN); 它非常适合当前的问题,但是该方法使 ScrollView 从整个屏幕上获取焦点,如果每次都使 AutoScroll 发生,则 EditText 将无法从用户那里接收信息,我的解决方案是使用不同的代码在可运行下:

sv.scrollTo(0, sv.getBottom() + sv.getScrollY());

sv.scrollTo(0, sv.getBottom() + sv.getScrollY());

making the same without losing focus on important views

在不失去对重要观点的关注的情况下做同样的事情

greetings.

你好。

回答by Ohiovr

I got this to work to scroll to the bottom of a ScrollView (with a TextView inside):

我让这个工作滚动到 ScrollView 的底部(里面有一个 TextView):

(I put this on a method that updates the TextView)

(我把它放在一个更新 TextView 的方法上)

final ScrollView myScrollView = (ScrollView) findViewById(R.id.myScroller);
    myScrollView.post(new Runnable() {
    public void run() {
        myScrollView.fullScroll(View.FOCUS_DOWN);
    }
});

回答by Swas_99

Adding another answer that does not involve coordinates.

添加另一个不涉及坐标的答案。

This will bring your desired view to focus (but not to the top position) :

这将使您想要的视图聚焦(但不是顶部位置):

  yourView.getParent().requestChildFocus(yourView,yourView);

public void RequestChildFocus (View child, View focused)

public void RequestChildFocus(查看子项,查看焦点)

child- The child of this ViewParent that wants focus. This view will contain the focused view. It is not necessarily the view that actually has focus.

child- 想要获得焦点的这个 ViewParent 的孩子。该视图将包含聚焦视图。实际具有焦点的不一定是视图。

focused- The view that is a descendant of child that actually has focus

focused- 视图是实际上具有焦点的 child 的后代