ScrollView 與ListView共存會存在滾動的問題

ScrollView 與ListView共存會存在滾動的問題,而且ListView只顯示一個半Item。 當ListView的高度設定必定的值時,ListView一樣地會顯示對應的高度的Item。 所以咱們能夠計算出這個ListView的總高度,再將它設置到ListView中,那麼以前的滾動,高度問題也就不存在了。 android


補充一點: 當你的listView每一個item是肯定高度,就可使用如下的方法. 可是假如你的item不是肯定高度,是會變化的.當 你的Item 裏面有個textView是包括寬度(就是用戶輸入過多的文字,致使換行.). 建議不要使用下面的方法. ide

totalHeight += listItem.getMeasuredHeight();  這行代碼,只會根據第一行出現的寬度計算下面的item,當其中有一個不一樣第一行的寬度,ScrollView 會不適應當前的高度. 佈局


代碼片斷(4)

[代碼] 獲取並設置ListView高度的方法

/** 本身補充的 spa

   最好在adapter.notifyDataSetChanged()以後再使用setListViewHeightBasedOnChildren,這個方法是計算你的listView的總高度,若是你的ListView沒數據,在使用這個setListViewHeightBasedOnChildren,就會只有空LIstView的高度! 因此每次使用adapter.notifyDataSetChanged() 都使用setListViewHeightBasedOnChildren. .net

ScrollView 默認layoutHeight是wrap_content的 你也改不了,並且子節點只能有一個. code

*/ orm

01 public void setListViewHeightBasedOnChildren(ListView listView) { 
02     ListAdapter listAdapter = listView.getAdapter();  
03     if(listAdapter ==null) { 
04         return; 
05     } 
06  
07     inttotalHeight =0; 
08     for(inti =0; i < listAdapter.getCount(); i++) { 
09         View listItem = listAdapter.getView(i,null, listView); 
10         listItem.measure(0,0); 
11         totalHeight += listItem.getMeasuredHeight(); 
12     } 
13  
14     ViewGroup.LayoutParams params = listView.getLayoutParams(); 
15     params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() -1)); 
16     ((MarginLayoutParams)params).setMargins(10,10,10,10);    //設置maring .
17     listView.setLayoutParams(params); 
18 }

[代碼] XML佈局

01 <ScrollView
02         android:layout_width="fill_parent"
03         android:layout_height="fill_parent"
04         android:fadingEdge ="none"
05         android:background="#FFF4F4F4"
06         xmlns:android="http://schemas.android.com/apk/res/android"
07         >
08    <LinearLayout
09     android:gravity="center_horizontal"
10     android:orientation="vertical"
11     android:background="#fff4f4f4"
12     android:layout_width="fill_parent"
13     android:layout_height="fill_parent"
14     >
15     <ListView
16         android:id="@+id/moreItemsListView"
17         android:layout_width="fill_parent"
18         android:layout_height="fill_parent"
19         android:cacheColorHint="#FFF4F4F4"
20         android:dividerHeight="0.0dip"
21         android:fadingEdge="none"
22         />
23    </LinearLayout>
24 </ScrollView>



[文件] android_listView.rar ~ 911KB    下載(0)

相關文章
相關標籤/搜索