博客
关于我
scrollView和listView的滑动冲突和listView显示不全
阅读量:768 次
发布时间:2019-03-24

本文共 2199 字,大约阅读时间需要 7 分钟。

ListView与ScrollView嵌套时的解决方案

在开发过程中,我们常需要将ListView与ScrollView嵌套使用。然而外层为ScrollView时,ListView上的item可能会出现显示异常:即使name列有多行项目,也只能显示一条。这一问题的成因主要是 ScrollView与ListView的焦点获取Conflict,所以我们需要采取特定措施进行修复。

问题分析

当外层控件是ScrollView时,滚动动作优先由ListView处理,这会导致ListView在尝试滑动时与ScrollView发生Conflict。要解决这个问题,我们可以采取以下措施:

  • 移除ListView的滑动属性:首先需要确保ListView自身不具备滑动功能,这可以通过移除ListView的滚动属性来实现。
  • 禁止ListView滑动:从Android版本字符中查找ListView相关的ID,并设置其滑动属性禁止。
  • 计算ListView的总高度:在ScrollView内布局ListView时,需要正确计算ListView中所有项目的总高度,以便不影响外层ScrollView的控制。
  • 实现方案代码

    下面是直接可以复制并使用的完整代码实现:

    package com.jgkj.bxxc.tools;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.ListAdapter;import android.widget.ListView;public class Scroll_ListView_Conflict extends ListView {    public Scroll_ListView_Conflict(Context context) {        super(context);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);        super.onMeasure(widthMeasureSpec, expandSpec);    }    public static void setListViewHeightBasedOnChildren(ListView listView) {        // 获取ListView对应的Adapter        ListAdapter listAdapter = listView.getAdapter();        if (listAdapter != null) {            int totalHeight = 0;            for (int i = 0; i < listAdapter.getCount(); i++) {                // 遍历所有子项                View listItem = listAdapter.getView(i, null, listView);                listItem.measure(0, 0); // 测量子项的尺寸                totalHeight += listItem.getMeasuredHeight() + 45; // 总高度累加            }            // 计算总高度并设定ListView参数            ViewGroup.LayoutParams params = listView.getLayoutParams();            params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));            listView.setLayoutParams(params);        }    }}

    操作说明

    该代码实现了解决ListView与ScrollViewConflict的关键方法:

    • onMeasure方法:通过设置动态尺寸确保ListView可以扩展性地适应外部容器。
    • setListViewHeightBasedOnChildren方法
      • 获取ListView的Adapter
      • 遍历所有子项,累加每个子项的高度(加上分割线高度)。
      • 根据总高度和分割线高度设定ListView的布局参数。

    注意事项

    • 在不同的Android版本中, ListView滑动属性的获取方式可能有所不同。请根据具体需求查找ListView的ID。
    • 如果需要确保ListView完全符合外部ScrollView布局,最好手动设置ScrollBar属性禁止显示。

    转载地址:http://taukk.baihongyu.com/

    你可能感兴趣的文章
    mysql 插入是否成功_PDO mysql:如何知道插入是否成功
    查看>>
    Mysql 数据库InnoDB存储引擎中主要组件的刷新清理条件:脏页、RedoLog重做日志、Insert Buffer或ChangeBuffer、Undo Log
    查看>>
    mysql 数据库中 count(*),count(1),count(列名)区别和效率问题
    查看>>
    mysql 数据库备份及ibdata1的瘦身
    查看>>
    MySQL 数据库备份种类以及常用备份工具汇总
    查看>>
    mysql 数据库存储引擎怎么选择?快来看看性能测试吧
    查看>>
    MySQL 数据库操作指南:学习如何使用 Python 进行增删改查操作
    查看>>
    MySQL 数据库的高可用性分析
    查看>>
    MySQL 数据库设计总结
    查看>>
    Mysql 数据库重置ID排序
    查看>>
    Mysql 数据类型一日期
    查看>>
    MySQL 数据类型和属性
    查看>>
    mysql 敲错命令 想取消怎么办?
    查看>>
    Mysql 整形列的字节与存储范围
    查看>>
    mysql 断电数据损坏,无法启动
    查看>>
    MySQL 日期时间类型的选择
    查看>>
    Mysql 时间操作(当天,昨天,7天,30天,半年,全年,季度)
    查看>>
    MySQL 是如何加锁的?
    查看>>
    MySQL 是怎样运行的 - InnoDB数据页结构
    查看>>
    mysql 更新子表_mysql 在update中实现子查询的方式
    查看>>