Get the App
SLTechnology News&Howtos  ›  Development  › 

How to control and prevent ScrollView from sliding to the bottom automatically in Android

Shulou Source: shulou.com Published: 2022-05-31 23:52:08 10月03日 Update

This article introduces the relevant knowledge of "how to control and prohibit ScrollView from sliding to the bottom automatically in Android". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Android controls the ScrollView to scroll to the bottom

There are two ways to do this. First, use scrollTo ():

Public static void scrollToBottom (final View scroll, final View inner) {Handler mHandler = new Handler (); mHandler.post (new Runnable () {public void run () {if (scroll = = null | | inner = = null) {return;} int offset = inner.getMeasuredHeight ()-scroll.getHeight (); if (offset < 0) {offset = 0;} scroll.scrollTo (0, offset);});}

The first implementation is relatively troublesome, and it is more recommended to use the second way, using fullScrol ()

Let's take a look at this function:

ScrollView.fullScroll (ScrollView.FOCUS_DOWN); scroll to the bottom

ScrollView.fullScroll (ScrollView.FOCUS_UP); scroll to the top

It should be noted that this method cannot be called directly.

Because many functions of Android are synchronized based on message queues, you need an operation. After addView, it does not mean that it will be displayed immediately, but it will wait in the queue for processing. Although it is very fast, if you call fullScroll immediately, view may not be displayed yet, so it will fail.

Should be updated in a new thread through handler

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

Prohibit ScrollView from automatically sliding to the bottom

But sometimes we need to prevent ScrollView from automatically sliding to the bottom. Here is the solution:

Concrete performance

When ScrollView nests GridView, ListView and other similar controls, the interface is refreshed when you get data from the network. What happens is that ScrollView automatically slides to the lowest end of the screen, specifically, it slides to show the position of the last piece of data. If you pull down and refresh at this time, the layout display will be unreasonable.

Why is that?

ChildView has the ability to gain focus.

This problem occurs because the childView is larger than the screen size and has the ability to gain focus. Because it cannot be resized, it can only be prevented from gaining focus. The basic ideas are to cancel its ability to gain focus, let ScrovView intercept its focus, and so on.

How to solve

Let the focus of childView be intercepted.

Concrete plan

Android:descendantFocusability= "blocksDescendants" is added to the LinearLayout under ScrollView

This is the end of the introduction to "how to control and disable ScrollView automatic sliding to the bottom in Android". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Focus control ability content function size screen situation data method more knowledge queue unreasonable practical successful learning next location low end method Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Tech Info Redmi Shulou Information Linux