標籤:android textview
一、在Android,一個單獨的TextView是無法滾動的,需要放在一個ScrollView中。
ScrollView提供了一系列的函數,其中fullScroll用來實現FOCUS_UP和FOCUS_DOWN鍵的功能,也就是滾動到頂部和底部。
如果在TextView的append後面馬上調用fullScroll,會發現無法滾動到真正的底部,這是因為Android下很多函數都是基於訊息的,用訊息佇列來保證同步,所以函數調用多數是非同步作業的。
有訊息佇列是非同步,訊息佇列先滾動到底部,然後textview的append方法顯示。所以無法正確滾動到底部。
解決辦法:
finalScrollView scrollView = (ScrollView) findViewById(R.id.scrollView1); if (scrollView != null) { scrollView.post(new Runnable() { publicvoidrun() { 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));
}
PS:對於APP安全檢測一般我都會用:www.ineice.com
淺談:Android TextView的append方法與捲軸同時使用