The Append method of the Android TextView is used in conjunction with the scroll bar

Source: Internet
Author: User
Tags message queue

One, on Android, a single textview is not scrollable and needs to be placed in a scrollview.
ScrollView provides a series of functions where Fullscroll is used to implement FOCUS_UP and Focus_down keys, that is, scrolling to the top and bottom.

If you call Fullscroll immediately after TextView append, you will find that you cannot scroll to the real bottom because many of the functions on Android are message-based and Message Queuing guarantees synchronization, so function calls are mostly asynchronous operations.
There is a message queue that is asynchronous, the message queue scrolls to the bottom, and the TextView append method is displayed. So I can't scroll to the bottom correctly.

Workaround:

Final ScrollView ScrollView = (ScrollView) Findviewbyid (r.id.scrollview1);         if NULL {            Scrollview.post () {new  Runnable ()                 {publicvoid  Run ( {                     scrollview.fullscroll (scrollview.focus_down);}                 }         );         }

The ListView is used with the scroll bar, prevents the ListView from scrolling, and scrolls to the bottom of the listview using the scroll bar.

Change the sentence in the above code run to this scrollview.scrollto (0, Mlistviewlist.getheight ());

Third, the ListView internal height calculation function

When the ListView is used with a vertical scroll bar, use only external scrollview instead of using the ListView to scroll. The following function is required to calculate the current height of the ListView.

     Public Static voidRecallistviewheightbasedonchildren (ListView listview) {if(ListView = =NULL)return; ListAdapter ListAdapter=Listview.getadapter (); if(ListAdapter = =NULL)return; intNtotalheight = 0;  for(inti = 0; I < Listadapter.getcount (); i++) {View ListItem= Listadapter.getview (I,NULL, ListView); Listitem.measure (0, 0); Ntotalheight+=listitem.getmeasuredheight (); } viewgroup.layoutparams params=Listview.getlayoutparams (); Params.height= Ntotalheight + (listview.getdividerheight () * (Listadapter.getcount ()-1)); }

The Append method of the Android TextView is used in conjunction with the scroll bar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.