scrollview中嵌套listview只显示一行的解决方案
来源:互联网 发布:盛世协同 软件 编辑:程序博客网 时间:2024/05/28 11:49
在开发的过程当中,由于手机屏幕的大小的限制,我们经常需要使用滑动的方式,来显示更多的内容。在最近的工作中,遇见一个需求,需要将ListView嵌套到ScrollView中显示。于是乎有了如下布局:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#FFE1FF"
- android:orientation="vertical" >
- <ScrollView
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <ListView
- android:id="@+id/listView1"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:fadingEdge="vertical"
- android:fadingEdgeLength="5dp" />
- </LinearLayout>
- </ScrollView>
- </LinearLayout>
运行程序结果是无论你如何调整layout_width,layout_height属性,ListView列表只显示一列!
在查阅的各种文档和资料后,发现在ScrollView中嵌套ListView空间,无法正确的计算ListView的大小,故可以通过代码,根据当前的ListView的列表项计算列表的尺寸。实现代码如下:
- public class MainActivity extends Activity {
- private ListView listView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- listView = (ListView) findViewById(R.id.listView1);
- String[] data = new String[] { "黄瓜", "橙子",… … "葡萄"};
- listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data));
- setListViewHeightBasedOnChildren(listView);
- }
- /**
* 当scrollview内部嵌套listview时,listview无论怎么设置都只会显示一行,只能通过动态的计算listview的
* 高度来展示listview,以下便是计算方法
*
* @param listView
*/ - public void setListViewHeightBasedOnChildren(ListView listView) {
- // 获取ListView对应的Adapter
- ListAdapter listAdapter = listView.getAdapter();
- if (listAdapter == null) {
- return;
- }
- int totalHeight = 0;
- for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
- // listAdapter.getCount()返回数据项的数目
- View listItem = listAdapter.getView(i, null, listView);
- // 计算子项View 的宽高
- listItem.measure(0, 0);
- // 统计所有子项的总高度
- totalHeight += listItem.getMeasuredHeight();
- }
- ViewGroup.LayoutParams params = listView.getLayoutParams();
- params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
- // listView.getDividerHeight()获取子项间分隔符占用的高度
- // params.height最后得到整个ListView完整显示需要的高度
- listView.setLayoutParams(params);
- }
- }
阅读全文
1 0
- Android中ScrollView嵌套ListView只显示一行的解决方案
- scrollview中嵌套listview只显示一行的解决方案
- ScrollView嵌套ListView只显示一行解决方案
- ScrollView嵌套Listview只显示一行的解决方案
- ScrollView中嵌套ListView,只显示一行ListView信息的解决方案
- ScrollView中嵌套ListView,TextView只显示一行的高度解决方案
- ScrollView中嵌套GridView,ListView只显示一行的解决办法
- ScrollView中嵌套GridView,ListView只显示一行的解决办法
- ScrollView中嵌套GridView,ListView只显示一行的解决办法
- ScrollView中嵌套GridView,ListView只显示一行的解决办法
- ListView嵌套在ScrollView中,只显示一行的解决办法
- ScrollView中嵌套ListView只显示一行的问题
- ScrollView中嵌套ListView只显示一行的解决方法。
- ScrollView中嵌套ListView,ListView只显示一行解决办法
- scrollview嵌套listview导致listview只显示一行的终极解决方案
- Android中解决scrollview中嵌套listview的listview只显示一行的问题
- ScrollView中嵌套ListView只显示一行问题
- ScrollView中嵌套ListView或GridView,只显示一行
- Linux CentOS 7 下 Apache Tomcat 7 安装与配置(心得)
- Redis集群使用总结2
- 3. Servlet read data from forms
- 古老的密码,紫书P73Uva1339
- 机器学习和数据挖掘(3):线性模型
- scrollview中嵌套listview只显示一行的解决方案
- 浅谈HTTP中Get与Post的区别
- java 判断文件是否为图片2
- NFC标签,Ntag标签,Ntag216芯片标签,超小尺寸,超薄
- 网页截图工具CutyCapt
- ubuntu: 终端全屏快捷键
- svn分支合并到主干
- Android 软键盘 解析
- python异常的抛出机制