The append method and the scroll bar of Android TextView are used at the same time. textviewappend

Source: Internet
Author: User

The append method and the scroll bar of Android TextView are used at the same time. textviewappend

1. In Android, a single TextView cannot be rolled and needs to be placed in a ScrollView.
ScrollView provides a series of functions. fullScroll is used to implement the FOCUS_UP and FOCUS_DOWN keys, that is, to scroll to the top and bottom.

If you call fullScroll immediately after the append of TextView, you will find that it cannot scroll to the real bottom. This is because many functions in Android are message-based and use message queue to ensure synchronization, therefore, most function calls are asynchronous.
The message queue is asynchronous. The message queue first scrolls to the bottom, and then the append method of textview is displayed. Therefore, you cannot scroll to the bottom correctly.

Solution:

final ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView1);        if (scrollView != null) {            scrollView.post(new Runnable() {                 public void run() {                     scrollView.fullScroll(ScrollView.FOCUS_DOWN);                 }         });         }

 

2. Use listview together with the scroll bar to prohibit the scrolling of the listview. Use the scroll bar to scroll to the bottom of the listview.

Replace the sentence in the run code above with this scrollView. scrollTo (0, mlistViewList. getHeight ());

 

3. listview internal height calculation function

When listview is used together with the vertical scroll bar, if only external scrollView is used, instead of listview scrolling is used. The following function is required to calculate the current height of the listview.

    public static void ReCalListViewHeightBasedOnChildren(ListView listView) {        if (listView == null) return;        ListAdapter listAdapter = listView.getAdapter();        if (listAdapter == null) return;        int nTotalHeight = 0;        for (int i = 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));    }

 

Related Article

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.