【转】在ScrollView中加入ListView时

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

<linearlayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content">  

    <textview 
                android:text="TestListViewHeader"
                android:id="@+id/headerTextView" 
                android:textsize="20sp" 
                android:layout_width="wrap_content"
                android:layout_height="30sp">  
    </textview>  
</linearlayout>  

 

2) 新建一个类,继承自LinearLayout用来显示上面的Layout:

      DemoListHeaderView.java

 

package com.zhang.test.view;

import com.zhang.test.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class DemoListHeaderView extends LinearLayout {

	private static final String TAG = "DemoListHeaderView";
	private Context context;
	private TextView textView;

	public DemoListHeaderView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initialize(context);
	}

	public DemoListHeaderView(Context context) {
		super(context);
		initialize(context);
	}

	private void initialize(Context context) {
		this.context = context;
		View view = LayoutInflater.from(this.context).inflate(R.layout.demo_list_item_header_view, null);
		textView = (TextView) view.findViewById(R.id.headerTextView);
		addView(view);
	}

	public void setTextView(String text) {
		textView.setText(text);
	}
}

 

之后在ListView设置setAdapter之前,一定要在setAdapter之前
加上代码:

        DemoListHeaderView headerView = new DemoListHeaderView(context);
        headerView.setTextView("Header : ");
        listView.addHeaderView(headerView);

        DemoListHeaderView footerView = new DemoListHeaderView(context);
        footerView.setTextView("Footer : ");
        listView.addFooterView(footerView);

 

全部:

package com.zhang.test;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.ListView;

import com.zhang.test.view.DemoListHeaderView;
import com.zhang.test.view.DemoListItemView;
import com.zhang.test.view.adapter.DemoListAdapter;

public class demoActivity extends Activity {

	private static final String TAG = "demoActivity";
	private Context context;
	private ListView listView;
	private ArrayList<demolistitemview.data> datas;
	private DemoListAdapter datasAdapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        context = this;
        listView = (ListView) findViewById(R.id.listView);
        datas = new ArrayList<demolistitemview.data>();
        loadData();
        datasAdapter = new DemoListAdapter(context, datas);

        DemoListHeaderView headerView = new DemoListHeaderView(context);
        headerView.setTextView("Header : ");
        listView.addHeaderView(headerView);

        DemoListHeaderView footerView = new DemoListHeaderView(context);
        footerView.setTextView("Footer : ");
        listView.addFooterView(footerView);

        listView.setAdapter(datasAdapter);
    }

    private void loadData() {
    	DemoListItemView.Data d;
    	for(int i=0; i<10; i++) {
    		d = new DemoListItemView.Data();
    		d.topText = "测试top";
    		d.bottomText = "测试bottom";
    		datas.add(d);
    	}
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值