Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report