博客
关于我
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/

    你可能感兴趣的文章
    Plotly 绘制表面 3D 未显示
    查看>>
    Plotly-Dash 存在未知问题并创建“加载依赖项时出错“;通过使用 Python-pandas.date_range
    查看>>
    Plotly-Dash:如何过滤具有多个数据框列的仪表板?
    查看>>
    Plotly:如何为 x 轴上的时间序列设置主要刻度线/网格线的值?
    查看>>
    Plotly:如何从 x 轴删除空日期?
    查看>>
    Plotly:如何从单条迹线制作堆积条形图?
    查看>>
    Plotly:如何以 Root 样式绘制直方图,仅显示直方图的轮廓?
    查看>>
    Plotly:如何使用 Plotly Express 组合散点图和线图?
    查看>>
    Plotly:如何使用 plotly.graph_objects 和 plotly.express 定义图形中的颜色?
    查看>>
    Plotly:如何使用 Python 对绘图对象条形图进行颜色编码?
    查看>>
    Plotly:如何使用 updatemenus 更新一个特定的跟踪?
    查看>>
    Plotly:如何使用长格式或宽格式的 pandas 数据框制作线图?
    查看>>
    Plotly:如何向烛台图添加交易量
    查看>>
    Plotly:如何在 plotly express 中找到趋势线的系数?
    查看>>
    Plotly:如何在桑基图中设置节点位置?
    查看>>
    Plotly:如何处理重叠的颜色条和图例?
    查看>>
    Plotly:如何手动设置 plotly express 散点图中点的颜色?
    查看>>
    Plotly:如何结合 make_subplots() 和 ff.create_distplot()?
    查看>>
    Plotly:如何绘制累积的“步骤“;直方图?
    查看>>
    Quartz进一步学习与使用
    查看>>