在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>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值