ListView数据只有显示不全bug

2024-09-06 01:08

本文主要是介绍ListView数据只有显示不全bug,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Android编程中,ScrollView嵌套ListView时,会无法正确的计算ListView的大小。解决的办法有如下两种:

解决方案1:

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

      

    --------------------------------------------------------------------------------

    去除Scroll之前的XML:

    <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="#FFFFFF"   

        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/test_listView"   

                    android:layout_width="match_parent"   

                    android:layout_height="match_parent"   

                    android:fadingEdge="vertical"/>   

            </LinearLayout>   

        </ScrollView>   

    </LinearLayout>   

      

    --------------------------------------------------------------------------------

    去除ScrollView之后的XML:

    <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="#FFFFFF"   

        android:orientation="vertical" >   

            <LinearLayout   

                android:layout_width="match_parent"   

                android:layout_height="match_parent" >   

                <ListView   

                    android:id="@+id/test_listView"   

                    android:layout_width="match_parent"   

                    android:layout_height="match_parent"   

                    android:fadingEdge="vertical"  />   

            </LinearLayout>   

    </LinearLayout>   

    END

解决方案2:

  1. 1

    通过代码,根据当前的ListView的列表子项计算列表的高度:

    --------------------------------------------------------------

     public class MainActivity extends Activity {   

        private ListView listView;  

        private String[] adapterData = new String[] { "标题1", "标题2", "标题3"}; 

        

       @Override   

        protected void onCreate(Bundle savedInstanceState) {   

            super.onCreate(savedInstanceState);   

            setContentView(R.layout.mainActivity);   

            listView = (ListView) findViewById(R.id.test_listView);   

            listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,adapterData));   

            fixListViewHeight(listView);   

        }   

       

        public void fixListViewHeight(ListView listView) {   

            // 如果没有设置数据适配器,则ListView没有子项,返回。  

            ListAdapter listAdapter = listView.getAdapter();  

            int totalHeight = 0;   

            if (listAdapter == null) {   

                return;   

            }   

            for (int index = 0, len = listAdapter.getCount(); i < len; i++) {     

                View listViewItem = listAdapter.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() * (listAdapter.getCount() - 1));   

            listView.setLayoutParams(params);   

        }   

    }   

这篇关于ListView数据只有显示不全bug的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1140572

相关文章

Java利用JSONPath操作JSON数据的技术指南

《Java利用JSONPath操作JSON数据的技术指南》JSONPath是一种强大的工具,用于查询和操作JSON数据,类似于SQL的语法,它为处理复杂的JSON数据结构提供了简单且高效... 目录1、简述2、什么是 jsONPath?3、Java 示例3.1 基本查询3.2 过滤查询3.3 递归搜索3.4

MySQL大表数据的分区与分库分表的实现

《MySQL大表数据的分区与分库分表的实现》数据库的分区和分库分表是两种常用的技术方案,本文主要介绍了MySQL大表数据的分区与分库分表的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有... 目录1. mysql大表数据的分区1.1 什么是分区?1.2 分区的类型1.3 分区的优点1.4 分

Mysql删除几亿条数据表中的部分数据的方法实现

《Mysql删除几亿条数据表中的部分数据的方法实现》在MySQL中删除一个大表中的数据时,需要特别注意操作的性能和对系统的影响,本文主要介绍了Mysql删除几亿条数据表中的部分数据的方法实现,具有一定... 目录1、需求2、方案1. 使用 DELETE 语句分批删除2. 使用 INPLACE ALTER T

Python Dash框架在数据可视化仪表板中的应用与实践记录

《PythonDash框架在数据可视化仪表板中的应用与实践记录》Python的PlotlyDash库提供了一种简便且强大的方式来构建和展示互动式数据仪表板,本篇文章将深入探讨如何使用Dash设计一... 目录python Dash框架在数据可视化仪表板中的应用与实践1. 什么是Plotly Dash?1.1

Redis 中的热点键和数据倾斜示例详解

《Redis中的热点键和数据倾斜示例详解》热点键是指在Redis中被频繁访问的特定键,这些键由于其高访问频率,可能导致Redis服务器的性能问题,尤其是在高并发场景下,本文给大家介绍Redis中的热... 目录Redis 中的热点键和数据倾斜热点键(Hot Key)定义特点应对策略示例数据倾斜(Data S

Python实现将MySQL中所有表的数据都导出为CSV文件并压缩

《Python实现将MySQL中所有表的数据都导出为CSV文件并压缩》这篇文章主要为大家详细介绍了如何使用Python将MySQL数据库中所有表的数据都导出为CSV文件到一个目录,并压缩为zip文件到... python将mysql数据库中所有表的数据都导出为CSV文件到一个目录,并压缩为zip文件到另一个

SpringBoot整合jasypt实现重要数据加密

《SpringBoot整合jasypt实现重要数据加密》Jasypt是一个专注于简化Java加密操作的开源工具,:本文主要介绍详细介绍了如何使用jasypt实现重要数据加密,感兴趣的小伙伴可... 目录jasypt简介 jasypt的优点SpringBoot使用jasypt创建mapper接口配置文件加密

使用Python高效获取网络数据的操作指南

《使用Python高效获取网络数据的操作指南》网络爬虫是一种自动化程序,用于访问和提取网站上的数据,Python是进行网络爬虫开发的理想语言,拥有丰富的库和工具,使得编写和维护爬虫变得简单高效,本文将... 目录网络爬虫的基本概念常用库介绍安装库Requests和BeautifulSoup爬虫开发发送请求解

Oracle存储过程里操作BLOB的字节数据的办法

《Oracle存储过程里操作BLOB的字节数据的办法》该篇文章介绍了如何在Oracle存储过程中操作BLOB的字节数据,作者研究了如何获取BLOB的字节长度、如何使用DBMS_LOB包进行BLOB操作... 目录一、缘由二、办法2.1 基本操作2.2 DBMS_LOB包2.3 字节级操作与RAW数据类型2.

MySQL使用binlog2sql工具实现在线恢复数据功能

《MySQL使用binlog2sql工具实现在线恢复数据功能》binlog2sql是大众点评开源的一款用于解析MySQLbinlog的工具,根据不同选项,可以得到原始SQL、回滚SQL等,下面我们就来... 目录背景目标步骤准备工作恢复数据结果验证结论背景生产数据库执行 SQL 脚本,一般会经过正规的审批