Android之将ListView嵌套到ScrollView中只显示一行的问题
来源:互联网 发布:主升浪选股软件 编辑:程序博客网 时间:2024/05/30 04:18
将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[] adapterData = new String[] { "Afghanistan", "Albania",… … "Bosnia"};
- listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,adapterData));
- setListViewHeightBasedOnChildren(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);
- }
- }
0 0
- Android之将ListView嵌套到ScrollView中只显示一行的问题
- ScrollView中嵌套ListView只显示一行的问题
- Android中解决scrollview中嵌套listview的listview只显示一行的问题
- Android中ScrollView嵌套ListView只显示一行的解决方案
- ScrollView中嵌套ListView只显示一行问题
- ScrollView嵌套ListView只显示一行的问题
- ScrollView嵌套ListView只显示一行的问题
- 解决ScrollView嵌套ListView只显示一行的问题
- ScrollView嵌套ListView只显示一行的问题
- scrollview嵌套listview只显示一行的问题
- ScrollView中嵌套GridView,ListView只显示一行的解决办法
- ScrollView中嵌套GridView,ListView只显示一行的解决办法
- ScrollView中嵌套GridView,ListView只显示一行的解决办法
- ScrollView中嵌套GridView,ListView只显示一行的解决办法
- ListView嵌套在ScrollView中,只显示一行的解决办法
- scrollview中嵌套listview只显示一行的解决方案
- ScrollView中嵌套ListView只显示一行的解决方法。
- MeasureSpec学习(解决ScrollView中嵌套ListView,ListView只显示一行的问题)
- CSU 1402 Fibonacci Multiply
- ZOJ3816--Generalized Palindromic Number(DFS)
- Java栈与堆
- 黑马程序员——33集合
- plsql登录时报错:Initialization error orcle not properly installed
- Android之将ListView嵌套到ScrollView中只显示一行的问题
- fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt
- 混杂设备驱动模型
- 懒人必备之Android效率开发框架(伪原创)
- 谈谈spring中的拦截器interceptor
- Zigbee协议栈内核分析 - 按键分析
- DEV-C++
- UITabBarItem 只有图片,没有文字,图片居中
- 基于Java自带同步机制实现生产者-消费者模式