淺談:Android TextView的append方法與捲軸同時使用

來源:互聯網
上載者:User

標籤: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方法與捲軸同時使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.