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

相关文章

canal实现mysql数据同步的详细过程

《canal实现mysql数据同步的详细过程》:本文主要介绍canal实现mysql数据同步的详细过程,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的... 目录1、canal下载2、mysql同步用户创建和授权3、canal admin安装和启动4、canal

使用SpringBoot整合Sharding Sphere实现数据脱敏的示例

《使用SpringBoot整合ShardingSphere实现数据脱敏的示例》ApacheShardingSphere数据脱敏模块,通过SQL拦截与改写实现敏感信息加密存储,解决手动处理繁琐及系统改... 目录痛点一:痛点二:脱敏配置Quick Start——Spring 显示配置:1.引入依赖2.创建脱敏

详解如何使用Python构建从数据到文档的自动化工作流

《详解如何使用Python构建从数据到文档的自动化工作流》这篇文章将通过真实工作场景拆解,为大家展示如何用Python构建自动化工作流,让工具代替人力完成这些数字苦力活,感兴趣的小伙伴可以跟随小编一起... 目录一、Excel处理:从数据搬运工到智能分析师二、PDF处理:文档工厂的智能生产线三、邮件自动化:

RedisTemplate默认序列化方式显示中文乱码的解决

《RedisTemplate默认序列化方式显示中文乱码的解决》本文主要介绍了SpringDataRedis默认使用JdkSerializationRedisSerializer导致数据乱码,文中通过示... 目录1. 问题原因2. 解决方案3. 配置类示例4. 配置说明5. 使用示例6. 验证存储结果7.

Python数据分析与可视化的全面指南(从数据清洗到图表呈现)

《Python数据分析与可视化的全面指南(从数据清洗到图表呈现)》Python是数据分析与可视化领域中最受欢迎的编程语言之一,凭借其丰富的库和工具,Python能够帮助我们快速处理、分析数据并生成高质... 目录一、数据采集与初步探索二、数据清洗的七种武器1. 缺失值处理策略2. 异常值检测与修正3. 数据

pandas实现数据concat拼接的示例代码

《pandas实现数据concat拼接的示例代码》pandas.concat用于合并DataFrame或Series,本文主要介绍了pandas实现数据concat拼接的示例代码,具有一定的参考价值,... 目录语法示例:使用pandas.concat合并数据默认的concat:参数axis=0,join=

C#代码实现解析WTGPS和BD数据

《C#代码实现解析WTGPS和BD数据》在现代的导航与定位应用中,准确解析GPS和北斗(BD)等卫星定位数据至关重要,本文将使用C#语言实现解析WTGPS和BD数据,需要的可以了解下... 目录一、代码结构概览1. 核心解析方法2. 位置信息解析3. 经纬度转换方法4. 日期和时间戳解析5. 辅助方法二、L

使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)

《使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)》字体设计和矢量图形处理是编程中一个有趣且实用的领域,通过Python的matplotlib库,我们可以轻松将字体轮廓... 目录背景知识字体轮廓的表示实现步骤1. 安装依赖库2. 准备数据3. 解析路径指令4. 绘制图形关键

解决mysql插入数据锁等待超时报错:Lock wait timeout exceeded;try restarting transaction

《解决mysql插入数据锁等待超时报错:Lockwaittimeoutexceeded;tryrestartingtransaction》:本文主要介绍解决mysql插入数据锁等待超时报... 目录报错信息解决办法1、数据库中执行如下sql2、再到 INNODB_TRX 事务表中查看总结报错信息Lock

使用C#删除Excel表格中的重复行数据的代码详解

《使用C#删除Excel表格中的重复行数据的代码详解》重复行是指在Excel表格中完全相同的多行数据,删除这些重复行至关重要,因为它们不仅会干扰数据分析,还可能导致错误的决策和结论,所以本文给大家介绍... 目录简介使用工具C# 删除Excel工作表中的重复行语法工作原理实现代码C# 删除指定Excel单元