在用列表控件做一个“更多功能”的界面的时候
1 <?xml version="1.0" encoding="utf-8"?> 2 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 3 android:id="@+id/ScrollView" 4 android:layout_width="fill_parent" 5 android:layout_height="wrap_content" 6 android:scrollbars="vertical"> 7 8 <LinearLayout 9 android:layout_width="match_parent" 10 android:layout_height="fill_parent" 11 android:orientation="vertical" > 12 13 <ListView 14 android:id="@+id/list_more_top" 15 android:layout_width="match_parent" 16 android:layout_height="fill_parent" > 17 </ListView> 18 <ImageView 19 android:id="@+id/imageView1" 20 android:layout_width="fill_parent" 21 android:layout_height="wrap_content" 22 android:src="@drawable/divider" /> 23 <ListView 24 android:id="@+id/list_more_center" 25 android:layout_width="match_parent" 26 android:layout_height="wrap_content" > 27 </ListView> 28 <ImageView 29 android:id="@+id/imageView2" 30 android:layout_width="fill_parent" 31 android:layout_height="wrap_content" 32 android:src="@drawable/divider" /> 33 <ListView 34 android:id="@+id/list_more_buttom" 35 android:layout_width="match_parent" 36 android:layout_height="wrap_content" > 37 </ListView> 38 </LinearLayout> 39 </ScrollView>
原本用ScrollView嵌套ListView 但是在测试的时候 ListView列表只显示一列! 这不是我希望得到的
我希望的是可以整个拖动三个列表
于是上网查询 发现的问题所在
在ScrollView中嵌套ListView空间,无法正确的计算ListView的大小,导致只显示列表第一项
故可以通过代码,根据当前的ListView的列表项计算列表的尺寸。
1 package songsong; 2 3 import com.example.xqx_tea.R; 4 5 import android.app.Activity; 6 import android.app.AlertDialog; 7 import android.app.Notification.Builder; 8 import android.content.DialogInterface; 9 import android.content.Intent; 10 import android.os.Bundle; 11 import android.app.AlertDialog; 12 import android.view.View; 13 import android.view.ViewGroup; 14 import android.view.Window; 15 import android.widget.AdapterView; 16 import android.widget.AdapterView.OnItemClickListener; 17 import android.widget.ArrayAdapter; 18 import android.widget.ListAdapter; 19 import android.widget.ListView; 20 21 public class MenuMore extends Activity{ 22 ListView list_more_top; 23 ListView list_more_center; 24 ListView list_more_buttom; 25 26 @Override 27 protected void onCreate(Bundle savedInstanceState) { 28 // TODO Auto-generated method stub 29 super.onCreate(savedInstanceState); 30 requestWindowFeature(Window.FEATURE_NO_TITLE); 31 setContentView(R.layout.menu_more); 32 33 ListView list_more_top = (ListView) findViewById(R.id.list_more_top); 34 ListView list_more_center = (ListView) findViewById(R.id.list_more_center); 35 ListView list_more_buttom = (ListView) findViewById(R.id.list_more_buttom); 36 String[] adapterData_top = new String[] { "饮茶时刻表", "小茶叶大妙用","中国十大名茶"}; 37 list_more_top.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,adapterData_top)); 38 39 String[] adapterData_center = new String[] { "使用帮助", "意见反馈","软件介绍"}; 40 list_more_center.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,adapterData_center)); 41 42 String[] adapterData_buttom = new String[] { "检查更新", "退出软件"}; 43 list_more_buttom.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,adapterData_buttom)); 44 45 setListViewHeightBasedOnChildren(list_more_top); 46 setListViewHeightBasedOnChildren(list_more_center); 47 setListViewHeightBasedOnChildren(list_more_buttom); 48 49 50 51 //为列表视图中选中的项添加响应事件 52 list_more_top.setOnItemClickListener(new OnItemClickListener() { 53 54 @Override 55 public void onItemClick(AdapterView<?> parent, View arg1, int pos, 56 long id) { 57 // TODO Auto-generated method stub 58 59 } 60 }); 61 62 //为列表视图中选中的项添加响应事件 63 list_more_center.setOnItemClickListener(new OnItemClickListener() { 64 65 @Override 66 public void onItemClick(AdapterView<?> parent, View arg1, int pos, 67 long id) { 68 // TODO Auto-generated method stub 69 70 } 71 }); 72 73 //为列表视图中选中的项添加响应事件 74 list_more_buttom.setOnItemClickListener(new OnItemClickListener() { 75 76 @Override 77 public void onItemClick(AdapterView<?> parent, View arg1, int pos, 78 long id) { 79 80 // TODO Auto-generated method stub 81 if(1 == id) 82 { 83 finish(); 84 } 85 if(0 == id) 86 { 87 Intent intent = new Intent(); 88 intent.setClass(MenuMore.this, More_submitdeal.class); 89 startActivity(intent); 90 } 91 92 } 93 }); 94 } 95 96 public void setListViewHeightBasedOnChildren(ListView listView) { 97 // 获取ListView对应的Adapter 98 ListAdapter listAdapter = listView.getAdapter(); 99 if (listAdapter == null) { 100 return; 101 } 102 103 int totalHeight = 0; 104 for (int i = 0, len = listAdapter.getCount(); i < len; i++) { 105 // listAdapter.getCount()返回数据项的数目 106 View listItem = listAdapter.getView(i, null, listView); 107 // 计算子项View 的宽高 108 listItem.measure(0, 0); 109 // 统计所有子项的总高度 110 totalHeight += listItem.getMeasuredHeight(); 111 } 112 113 ViewGroup.LayoutParams params = listView.getLayoutParams(); 114 params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1)); 115 // listView.getDividerHeight()获取子项间分隔符占用的高度 116 // params.height最后得到整个ListView完整显示需要的高度 117 listView.setLayoutParams(params); 118 } 119 120 121 }
给三个列表分别求出ListView完全显示需要的高度 便可以整体上下滑动三个列表了
时间: 2024-12-14 04:58:42