ScrollView嵌套ListView只顯示一行之計算的高度不正確的解決辦法(轉)


ScrollView嵌套ListView只顯示一行之計算的高度不正確的解決辦法

分類: android應用開發

1、前言

從谷歌那里找到的ScrollView嵌套ListView只顯示一行的解決辦法相信很多人都遇到過,然后大部分都是用這位博主的辦法解決的吧

剛開始我也是用這個辦法解決的,首先感謝這位哥的大私奉獻,貼上地址

http://blog.csdn.net/p106786860/article/details/10461015

2、解決的核心代碼

 

[html]  view plain copy 在CODE上查看代碼片 派生到我的代碼片
 
  1. public void setListViewHeightBasedOnChildren(ListView listView) {     
  2.         // 獲取ListView對應的Adapter     
  3.         ListAdapter listAdapter = listView.getAdapter();     
  4.         if (listAdapter == null) {     
  5.             return;     
  6.         }     
  7.      
  8.         int totalHeight = 0;     
  9.         for (int i = 0, len = listAdapter.getCount(); i len; i++) {     
  10.             // listAdapter.getCount()返回數據項的數目     
  11.             View listItem = listAdapter.getView(i, null, listView);     
  12.             // 計算子項View 的寬高     
  13.             listItem.measure(0, 0);      
  14.             // 統計所有子項的總高度     
  15.             totalHeight += listItem.getMeasuredHeight();      
  16.         }     
  17.      
  18.         ViewGroup.LayoutParams params = listView.getLayoutParams();     
  19.         params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));     
  20.         // listView.getDividerHeight()獲取子項間分隔符占用的高度     
  21.         // params.height最后得到整個ListView完整顯示需要的高度     
  22.         listView.setLayoutParams(params);     
  23.     }     
這個代碼讓控件去計算Listview自己的高度然后設置這個Listview的高度

 

但是這個代碼里面有一個問題,就是這個當你的ListView里面有多行的TextView的話,ListView的高度就會計算錯誤,它只算到了一行TextView的高度,

這個問題在so上的概述為以下:

http://stackoverflow.com/questions/14386584/getmeasuredheight-of-textview-with-wrapped-text

3、終極解決辦法

這個問題頭疼了一陣后,查找了一下,應該重寫一個TextView的onMeasure方法比較好解決

代碼有

 

[java]  view plain copy 在CODE上查看代碼片 派生到我的代碼片
 
  1. @Override  
  2.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  3.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  4.   
  5.         Layout layout = getLayout();  
  6.         if (layout != null) {  
  7.             int height = (int)FloatMath.ceil(getMaxLineHeight(this.getText().toString()))  
  8.                     + getCompoundPaddingTop() + getCompoundPaddingBottom();  
  9.             int width = getMeasuredWidth();              
  10.             setMeasuredDimension(width, height);  
  11.         }  
  12.     }  
  13.   
  14.     private float getMaxLineHeight(String str) {  
  15.         float height = 0.0f;  
  16.         float screenW = ((Activity)context).getWindowManager().getDefaultDisplay().getWidth();  
  17.         float paddingLeft = ((LinearLayout)this.getParent()).getPaddingLeft();  
  18.         float paddingReft = ((LinearLayout)this.getParent()).getPaddingRight();  
  19. //這里具體this.getPaint()要注意使用,要看你的TextView在什么位置,這個是拿TextView父控件的Padding的,為了更准確的算出換行  
  20.  int line = (int) Math.ceil( (this.getPaint().measureText(str)/(screenW-paddingLeft-paddingReft))); height = (this.getPaint().getFontMetrics().descent-this.getPaint().getFontMetrics().ascent)*line; return height;}  


 

上面的代碼完成更能為,在ListView開始測量時,測量到TextView時,就調用我們的onMeasure方法,我們就可以測量字體的總寬度除與去掉邊距的屏幕的大小,就可以算出文字要幾行來顯示,然后測量字體的高度*行數可以得到字體的總高度,然后在加上上下邊距就是TextView真正的高度,然后setMeasuredDimension進去就可以計算出正確的值出來。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM