第一种方法:
在计算listview总高度并设置
使用该方法需要注意:子ListView的每个Item必须是LinearLayout,不能是其他的,因为其他的Layout(如RelativeLayout)没有重写onMeasure(),所以会在onMeasure()时抛出异常。
ScrollView下只能有一个字布局
item布局
子ListView的每个Item必须是LinearLayout,不能是其他的,因为其他的Layout(如RelativeLayout)没有重写onMeasure(),所以会在onMeasure()时抛出异常。
MainActivity.java(红色为方法)
package com.bawei.day06_scrollview;import java.util.ArrayList;import java.util.List;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.view.View.MeasureSpec;import android.view.ViewGroup;import android.view.ViewGroup.LayoutParams;import android.widget.ArrayAdapter;import android.widget.BaseAdapter;import android.widget.ListView;public class MainActivity extends Activity { private ListView listView; private Listlist; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listview); list = new ArrayList (); for (int i = 0; i < 200; i++) { list.add(i+""); } BaseAdapter adapter=new Adper(MainActivity.this,list); listView.setAdapter(adapter); } //界面加载完 @Overridepublic void onWindowFocusChanged(boolean hasFocus) { // TODO Auto-generated method stub super.onWindowFocusChanged(hasFocus); setListViewHeightBasedOnChildren(listView);} public void setListViewHeightBasedOnChildren(ListView listView) { //BaseAdapter为listview布局 BaseAdapter listAdapter = (BaseAdapter) listView.getAdapter(); if (listAdapter == null) { return; } int totalHeight = 0; for (int i = 0; i < listAdapter.getCount(); i++) { View listItem = listAdapter.getView(i, null, listView); if(listItem != null){ listItem.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); listItem.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); totalHeight += listItem.getMeasuredHeight(); } } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)) + listView.getPaddingTop() + listView.getPaddingBottom(); listView.setLayoutParams(params); } }
适配器
package com.bawei.day06_scrollview;import java.util.List;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;public class Adper extends BaseAdapter { Context context; Listlist; public Adper(Context context, List list) { // TODO Auto-generated constructor stub this.context=context; this.list=list; } public int getCount() { // TODO Auto-generated method stub return list.size(); } public Object getItem(int position) { // TODO Auto-generated method stub return null; } public long getItemId(int position) { // TODO Auto-generated method stub return 0; } public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder viewHolder; if(convertView==null){ convertView=View.inflate(context, R.layout.listview, null); viewHolder=new ViewHolder(); viewHolder.textView=(TextView) convertView.findViewById(R.id.text); convertView.setTag(viewHolder); }else{ viewHolder=(ViewHolder) convertView.getTag(); } viewHolder.textView.setText(list.get(position)); return convertView; }class ViewHolder{ TextView textView;}}
第二种方法
自定义ListView,重载onMeasure()方法,设置全部显示
MainActivity.java
package com.bawei.day06;import java.util.ArrayList;import java.util.List;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.widget.ArrayAdapter;import android.widget.BaseAdapter;import android.widget.ListView;public class MainActivity extends Activity { private ListView listView; private Listlist; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listview); list = new ArrayList (); for (int i = 0; i < 200; i++) { list.add(i+""); } ArrayAdapter adapter=new ArrayAdapter (this, android.R.layout.simple_expandable_list_item_1,list); listView.setAdapter(adapter); } }
重写listview
ListViewForScrollView.java
package com.bawei.day06;import android.content.Context;import android.util.AttributeSet;import android.widget.ListView;public class ListViewForScrollView extends ListView { public ListViewForScrollView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } @Override /** * 重写该方法,达到使ListView适应ScrollView的效果 */ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); }}