`
天涯比邻海角
  • 浏览: 5413 次
文章分类
社区版块
存档分类
最新评论

浅谈:Android TextView的append方法与滚动条同时使用

 
阅读更多
一、在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));
}


PS:对于APP安全检测一般我都会用:http://www.ineice.com
2
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics