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