一、在Android,一個單獨的TextView是無法滾動的,需要放在一個ScrollView中。
ScrollView提供了一系列的函數,其中fullScroll用來實現FOCUS_UP和FOCUS_DOWN鍵的功能,也就是滾動到頂部和底部。
如果在TextView的append后面馬上調用fullScroll,會發現無法滾動到真正的底部,這是因為Android下很多函數都是基於消息的,用消息隊列來保證同步,所以函數調用多數是異步操作的。
有消息隊列是異步的,消息隊列先滾動到底部,然后textview的append方法顯示。所以無法正確滾動到底部。
解決辦法:
final ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView1); if (scrollView != null) { scrollView.post(new Runnable() { public void run() { scrollView.fullScroll(ScrollView.FOCUS_DOWN); } }); }
二、listview與滾動條一起使用,禁止listview的滾動,使用滾動條滾動到listview的底部
把上面代碼run里面那句換為這個scrollView.scrollTo(0, mlistViewList.getHeight());
三、listview內部高度計算函數
當listview與垂直滾動條一起使用時候,如果只用外部scrollView,而不使用listview滾動。需要下面的函數來計算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)); }