我可以在Android中以编程方式滚动滚动滚动滚动视图吗?

时间:2022-12-05 22:22:20

Is there any way to scroll a ScrollView programmatically to a certain position?

是否有方法以编程方式将滚动视图滚动到某个位置?

I have created dynamic TableLayout which 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?

是可能的吗?

15 个解决方案

#1


141  

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

or

sv.scrollTo(5, 10);

sv。scrollTo(5、10);

#2


182  

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

#3


34  

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:

但要注意的是,每次都有事情发生的时候,这个名字就会被调用。g如果你设置了一个不可见或类似的视图)所以如果你不再需要这个监听器,不要忘记删除它:

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

公共空间removeGlobalOnLayoutListener(ViewTreeObserver。on SDK (SDK) Lvl < 16

or

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

公共空间removeOnGlobalLayoutListener(ViewTreeObserver。在SDK中Lvl >= 16

#4


17  

Use something like this:

使用这样的:

mScrollView.scrollBy(10, 10);

or

mScrollView.scrollTo(10, 10);

#5


16  

There are a lot of good answers here, but I only want to add one thing. It sometimes happens that you want to scroll your ScrollView to a specific view of the layout, instead of a full scroll to the top or the bottom.

这里有很多好的答案,但我只想补充一点。有时,您希望将ScrollView滚动到布局的特定视图,而不是滚动到顶部或底部。

A simple example: in a registration form, if the user tap the "Signup" button when a edit text of the form is not filled, you want to scroll to that specific edit text to tell the user that he must fill that field.

一个简单的例子:在注册表单中,如果用户在未填充表单的编辑文本时点击“注册”按钮,就需要滚动到特定的编辑文本,告诉用户他必须填写该字段。

In that case, you can do something like that:

在这种情况下,你可以这样做:

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

or, if you want a smooth scroll instead of an instant scroll:

或者,如果你想要一个平滑的滚动,而不是一个即时的滚动:

scrollView.post(new Runnable() { 
            public void run() { 
                 scrollView.smoothScrollTo(0, editText.getBottom());
            } 
    });

Obviously you can use any type of view instead of Edit Text. Note that getBottom() returns the coordinates of the view based on its parent layout, so all the views used inside the ScrollView should have only a parent (for example a Linear Layout).

显然,您可以使用任何类型的视图,而不是编辑文本。注意,getBottom()根据视图的父布局返回视图的坐标,所以在ScrollView中使用的所有视图应该只有一个父视图(例如线性布局)。

If you have multiple parents inside the child of the ScrollView, the only solution i've found is to call requestChildFocus on the parent view:

如果在ScrollView的子节点中有多个父类,我找到的惟一解决方案是将requestChildFocus调用父视图:

editText.getParent().requestChildFocus(editText, editText);

but in this case you cannot have a smooth scroll.

但是在这种情况下,你不能有一个平滑的滚动。

I hope this answer can help someone with the same problem.

我希望这个答案能对有同样问题的人有所帮助。

#6


9  

Try using scrollTo method More Info

尝试使用scrollTo方法获取更多信息

#7


7  

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

#8


5  

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

我让它滚动到滚动视图的底部(里面有一个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);
    }
});

#9


4  

Yes, you can.

是的,你可以。

Let's say you got one Layout and inside that, you got many Views. So If you want to scroll to any View programmatically, you have to write following code snippet:

假设你有一个布局,里面有很多视图。因此,如果您想以编程方式滚动到任何视图,您必须编写以下代码片段:

Eg.

如。

content_main.xml

content_main.xml

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <Button
            android:id="@+id/btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/txtView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>
</ScrollView>

MainActivity.java

MainActivity.java

ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView);
Button btn = (Button) findViewById(R.id.ivEventBanner);
TextView txtView = (TextView) findViewById(R.id.ivEditBannerImage);

If you want to scroll to specific View let's say txtview in this case just write:

如果你想滚动到特定的视图比如txtview在这个例子中,你可以这样写:

scrollView.smoothScrollTo(txtView.getScrollX(),txtView.getScrollY());

And you are done..

你是做. .

#10


3  

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

#11


3  

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.

孩子——想要集中注意力的家长的孩子。此视图将包含焦点视图。它不一定是真正有焦点的观点。

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

焦点-实际上有焦点的孩子的后代的视图

#12


2  

**to scroll up to desired height. I have come up with some good solution **

**向上滚动到期望的高度。我想出了一些好办法

                scrollView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        scrollView.scrollBy(0, childView.getHeight());
                    }
                }, 100);

#13


1  

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从整个屏幕中获取焦点,如果每次都自动滚动,EditText将无法从用户那里接收信息,我的解决方案是在runnable下使用不同的代码:

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

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

making the same without losing focus on important views

同样,不要忽视重要的观点

greetings.

问候。

#14


1  

Everyone is posting such complicated answers.

每个人都在发布如此复杂的答案。

I found an easy answer, for scrolling to the bottom, nicely:

我找到了一个简单的答案,可以很好地滚动到底部:

final ScrollView myScroller = (ScrollView) findViewById(R.id.myScrollerView);

// Scroll views can only have 1 child, so get the first child's bottom,
// which should be the full size of the whole content inside the ScrollView
myScroller.smoothScrollTo( 0, myScroller.getChildAt( 0 ).getBottom() );

And, if necessary, you can put the second line of code, above, into a runnable:

如果有必要,你可以将上面第二行代码放入一个可运行的:

myScroller.post( new Runnable() {
    @Override
    public void run() {
        myScroller.smoothScrollTo( 0, myScroller.getChildAt( 0 ).getBottom() );
    }
}

It took me much research and playing around to find this simple solution. I hope it helps you, too! :)

我花了大量的时间研究,寻找这个简单的解决方案。我希望它也能帮助你!:)

#15


1  

just page scroll:

只是页面滚动:

ScrollView sv = (ScrollView) findViewById(your_scroll_view);
sv.pageScroll(View.FOCUS_DOWN);

#1


141  

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

or

sv.scrollTo(5, 10);

sv。scrollTo(5、10);

#2


182  

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

#3


34  

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:

但要注意的是,每次都有事情发生的时候,这个名字就会被调用。g如果你设置了一个不可见或类似的视图)所以如果你不再需要这个监听器,不要忘记删除它:

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

公共空间removeGlobalOnLayoutListener(ViewTreeObserver。on SDK (SDK) Lvl < 16

or

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

公共空间removeOnGlobalLayoutListener(ViewTreeObserver。在SDK中Lvl >= 16

#4


17  

Use something like this:

使用这样的:

mScrollView.scrollBy(10, 10);

or

mScrollView.scrollTo(10, 10);

#5


16  

There are a lot of good answers here, but I only want to add one thing. It sometimes happens that you want to scroll your ScrollView to a specific view of the layout, instead of a full scroll to the top or the bottom.

这里有很多好的答案,但我只想补充一点。有时,您希望将ScrollView滚动到布局的特定视图,而不是滚动到顶部或底部。

A simple example: in a registration form, if the user tap the "Signup" button when a edit text of the form is not filled, you want to scroll to that specific edit text to tell the user that he must fill that field.

一个简单的例子:在注册表单中,如果用户在未填充表单的编辑文本时点击“注册”按钮,就需要滚动到特定的编辑文本,告诉用户他必须填写该字段。

In that case, you can do something like that:

在这种情况下,你可以这样做:

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

or, if you want a smooth scroll instead of an instant scroll:

或者,如果你想要一个平滑的滚动,而不是一个即时的滚动:

scrollView.post(new Runnable() { 
            public void run() { 
                 scrollView.smoothScrollTo(0, editText.getBottom());
            } 
    });

Obviously you can use any type of view instead of Edit Text. Note that getBottom() returns the coordinates of the view based on its parent layout, so all the views used inside the ScrollView should have only a parent (for example a Linear Layout).

显然,您可以使用任何类型的视图,而不是编辑文本。注意,getBottom()根据视图的父布局返回视图的坐标,所以在ScrollView中使用的所有视图应该只有一个父视图(例如线性布局)。

If you have multiple parents inside the child of the ScrollView, the only solution i've found is to call requestChildFocus on the parent view:

如果在ScrollView的子节点中有多个父类,我找到的惟一解决方案是将requestChildFocus调用父视图:

editText.getParent().requestChildFocus(editText, editText);

but in this case you cannot have a smooth scroll.

但是在这种情况下,你不能有一个平滑的滚动。

I hope this answer can help someone with the same problem.

我希望这个答案能对有同样问题的人有所帮助。

#6


9  

Try using scrollTo method More Info

尝试使用scrollTo方法获取更多信息

#7


7  

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

#8


5  

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

我让它滚动到滚动视图的底部(里面有一个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);
    }
});

#9


4  

Yes, you can.

是的,你可以。

Let's say you got one Layout and inside that, you got many Views. So If you want to scroll to any View programmatically, you have to write following code snippet:

假设你有一个布局,里面有很多视图。因此,如果您想以编程方式滚动到任何视图,您必须编写以下代码片段:

Eg.

如。

content_main.xml

content_main.xml

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <Button
            android:id="@+id/btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/txtView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>
</ScrollView>

MainActivity.java

MainActivity.java

ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView);
Button btn = (Button) findViewById(R.id.ivEventBanner);
TextView txtView = (TextView) findViewById(R.id.ivEditBannerImage);

If you want to scroll to specific View let's say txtview in this case just write:

如果你想滚动到特定的视图比如txtview在这个例子中,你可以这样写:

scrollView.smoothScrollTo(txtView.getScrollX(),txtView.getScrollY());

And you are done..

你是做. .

#10


3  

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

#11


3  

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.

孩子——想要集中注意力的家长的孩子。此视图将包含焦点视图。它不一定是真正有焦点的观点。

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

焦点-实际上有焦点的孩子的后代的视图

#12


2  

**to scroll up to desired height. I have come up with some good solution **

**向上滚动到期望的高度。我想出了一些好办法

                scrollView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        scrollView.scrollBy(0, childView.getHeight());
                    }
                }, 100);

#13


1  

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从整个屏幕中获取焦点,如果每次都自动滚动,EditText将无法从用户那里接收信息,我的解决方案是在runnable下使用不同的代码:

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

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

making the same without losing focus on important views

同样,不要忽视重要的观点

greetings.

问候。

#14


1  

Everyone is posting such complicated answers.

每个人都在发布如此复杂的答案。

I found an easy answer, for scrolling to the bottom, nicely:

我找到了一个简单的答案,可以很好地滚动到底部:

final ScrollView myScroller = (ScrollView) findViewById(R.id.myScrollerView);

// Scroll views can only have 1 child, so get the first child's bottom,
// which should be the full size of the whole content inside the ScrollView
myScroller.smoothScrollTo( 0, myScroller.getChildAt( 0 ).getBottom() );

And, if necessary, you can put the second line of code, above, into a runnable:

如果有必要,你可以将上面第二行代码放入一个可运行的:

myScroller.post( new Runnable() {
    @Override
    public void run() {
        myScroller.smoothScrollTo( 0, myScroller.getChildAt( 0 ).getBottom() );
    }
}

It took me much research and playing around to find this simple solution. I hope it helps you, too! :)

我花了大量的时间研究,寻找这个简单的解决方案。我希望它也能帮助你!:)

#15


1  

just page scroll:

只是页面滚动:

ScrollView sv = (ScrollView) findViewById(your_scroll_view);
sv.pageScroll(View.FOCUS_DOWN);