Android中ScrollView嵌套ListView只显示一行的解决方案

来源:互联网 发布:linux内核设计及实现 编辑:程序博客网 时间:2024/05/19 17:09

Android中ScrollView嵌套ListView只显示一行的解决方案

解决方案1:

直接把包含ListView控件的ScrollView控件从布局文件中去除,留下ListView控件,这是最简单快捷的解决办法.
如果一定要在ScrollView中包含ListView,则参考

解决方案2:

public void showlist(){        List<HashMap<String, String>> dataHashMaps = new ArrayList<HashMap<String, String>>();        for (int i = 0; i < 8; i++)        {            HashMap<String, String> map = new HashMap<String, String>();            map.put("name", "张三" + i);            map.put("phone", "1333333333" + i);            dataHashMaps.add(map);        }        System.out.println(dataHashMaps.size());        SimpleAdapter adapter = new SimpleAdapter(this, dataHashMaps, R.layout.item, new String[]        { "name", "phone" }, new int[]        { R.id.name, R.id.phone });        ListView listView = (ListView) findViewById(R.id.list);        listView.setAdapter(adapter);        //处理方案:        int totalHeight = 0;        // 如果没有设置数据适配器,则ListView没有子项,返回。        for (int index = 0, len = adapter.getCount(); index < len; index++)        {            View listViewItem = adapter.getView(index, null, listView);            // 计算子项View 的宽高            listViewItem.measure(0, 0);            // 计算所有子项的高度和            totalHeight += listViewItem.getMeasuredHeight();        }        ViewGroup.LayoutParams params = listView.getLayoutParams();        // listView.getDividerHeight()获取子项间分隔符的高度        // params.height设置ListView完全显示需要的高度        params.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));        listView.setLayoutParams(params);    }
1 0
原创粉丝点击