在ScrollView中加入ListView時

from:http://daydayup1989.iteye.com/blog/905880

在ScrollView中加入ListView時,有個很棘手的BUG,就只能顯示出ListView的一行半左右。

 

ListView 本來是不應該 放在 ScrollView 裏的,Google員工 Roman Guy早已回覆:

"There is no need to put a ListView in a ScrollView since ListView already supports scrolling. Do NOT put a ListView inside a ScrollView. ListView already handles scrolling, you’re only going to run into trouble. "

 

我當然是不想自找麻煩,把ListView往ScrollView裏面加的,但是項目中有這個需求。

 

大致發現有以下幾種方法:

法一:

 

一個Activity,其中不只有ListView一個控件,還有其他的TextView、ImageView、Button等等很多控件,這樣很可能會 佔據屏幕很大一部分,要知道手機的屏幕只有480像素,通常的想法是給這個Activity加上一個ScrollView,讓其有滾動條,但是會發現 ListView的高度不是隨着內容而自動填充的。那麼我們可以使用ListView的addHeaderView 以及addFooterView 爲ListView增加上下的頭和尾,這樣就可以讓ListView填充到整個屏幕。

 

步驟:

1) 新建一個Layout:   demo_list_item_header_view.xml:

Xml代碼  收藏代碼
  1. <linearlayout   
  2.      xmlns:android="http://schemas.android.com/apk/res/android"   
  3.      android:layout_width="wrap_content"   
  4.      android:layout_height="wrap_content">    
  5.   
  6.     <textview   
  7.                 android:text="TestListViewHeader"  
  8.                 android:id="@+id/headerTextView"   
  9.                 android:textsize="20sp"   
  10.                 android:layout_width="wrap_content"  
  11.                 android:layout_height="30sp">    
  12.     </textview>    
  13. </linearlayout>    

 

2) 新建一個類,繼承自LinearLayout用來顯示上面的Layout:

      DemoListHeaderView.java

 

Java代碼  收藏代碼
  1. package com.zhang.test.view;  
  2.   
  3. import com.zhang.test.R;  
  4.   
  5. import android.content.Context;  
  6. import android.util.AttributeSet;  
  7. import android.view.LayoutInflater;  
  8. import android.view.View;  
  9. import android.widget.LinearLayout;  
  10. import android.widget.TextView;  
  11.   
  12. public class DemoListHeaderView extends LinearLayout {  
  13.   
  14.     private static final String TAG = "DemoListHeaderView";  
  15.     private Context context;  
  16.     private TextView textView;  
  17.   
  18.     public DemoListHeaderView(Context context, AttributeSet attrs) {  
  19.         super(context, attrs);  
  20.         initialize(context);  
  21.     }  
  22.   
  23.     public DemoListHeaderView(Context context) {  
  24.         super(context);  
  25.         initialize(context);  
  26.     }  
  27.   
  28.     private void initialize(Context context) {  
  29.         this.context = context;  
  30.         View view = LayoutInflater.from(this.context).inflate(R.layout.demo_list_item_header_view, null);  
  31.         textView = (TextView) view.findViewById(R.id.headerTextView);  
  32.         addView(view);  
  33.     }  
  34.   
  35.     public void setTextView(String text) {  
  36.         textView.setText(text);  
  37.     }  
  38. }  

 

之後在ListView設置setAdapter之前,一定要在setAdapter之前 
加上代碼:

Java代碼  收藏代碼
  1. DemoListHeaderView headerView = new DemoListHeaderView(context);  
  2. headerView.setTextView("Header : ");  
  3. listView.addHeaderView(headerView);  
  4.   
  5. DemoListHeaderView footerView = new DemoListHeaderView(context);  
  6. footerView.setTextView("Footer : ");  
  7. listView.addFooterView(footerView);  

 

全部:

Java代碼  收藏代碼
  1. package com.zhang.test;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.Context;  
  7. import android.os.Bundle;  
  8. import android.widget.ListView;  
  9.   
  10. import com.zhang.test.view.DemoListHeaderView;  
  11. import com.zhang.test.view.DemoListItemView;  
  12. import com.zhang.test.view.adapter.DemoListAdapter;  
  13.   
  14. public class demoActivity extends Activity {  
  15.   
  16.     private static final String TAG = "demoActivity";  
  17.     private Context context;  
  18.     private ListView listView;  
  19.     private ArrayList<demolistitemview.data> datas;  
  20.     private DemoListAdapter datasAdapter;  
  21.   
  22.     /** Called when the activity is first created. */  
  23.     @Override  
  24.     public void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.main);  
  27.         context = this;  
  28.         listView = (ListView) findViewById(R.id.listView);  
  29.         datas = new ArrayList<demolistitemview.data>();  
  30.         loadData();  
  31.         datasAdapter = new DemoListAdapter(context, datas);  
  32.   
  33.         DemoListHeaderView headerView = new DemoListHeaderView(context);  
  34.         headerView.setTextView("Header : ");  
  35.         listView.addHeaderView(headerView);  
  36.   
  37.         DemoListHeaderView footerView = new DemoListHeaderView(context);  
  38.         footerView.setTextView("Footer : ");  
  39.         listView.addFooterView(footerView);  
  40.   
  41.         listView.setAdapter(datasAdapter);  
  42.     }  
  43.   
  44.     private void loadData() {  
  45.         DemoListItemView.Data d;  
  46.         for(int i=0; i<10; i++) {  
  47.             d = new DemoListItemView.Data();  
  48.             d.topText = "測試top";  
  49.             d.bottomText = "測試bottom";  
  50.             datas.add(d);  
  51.         }  
  52.     }  
  53. }  
  54. </demolistitemview.data></demolistitemview.data>  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章