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

    你可能感兴趣的文章
    python读取字符串指定位置字符_python要怎么截取指定位置的字符串呢?
    查看>>
    python | scikit-llm,一个神奇的 Python 库!
    查看>>
    python | sentry,一个超酷的 关于错误监控工具 Python 库!
    查看>>
    python | shiv,一个超酷的 打包工具 Python 库!
    查看>>
    python | six,一个神奇的 Python 库!
    查看>>
    Python读取图片的几种方法供net使用
    查看>>
    python | spacy,一个神奇的 Python 库!
    查看>>
    python | sqlmap,一个实用的 Python 库!
    查看>>
    python | sumy,一个超酷的 用于文本摘要的 Python 库!
    查看>>
    python | tiler,一个不可思议的 图像切片重组 Python 库!
    查看>>
    python | tinydb,一个非常厉害的 关于数据库的 Python 库!
    查看>>
    python | tox,一个超强的 自动化测试工具 Python 库!
    查看>>
    python | ttkbootstrap,一个神奇的 Python 库!
    查看>>
    python | unoconv,一个超厉害的 Python 库!
    查看>>
    python | urllib3,一个超强的 Python 库!
    查看>>
    python | webassets,一个超强的 Python 库!
    查看>>
    python | werkzeug,一个不可思议的 Python 库!
    查看>>
    python | xlsxwriter,一个实用的 Python 库!
    查看>>
    python | xlwings,一个非常实用的 Excel 相关的 Python 库!
    查看>>
    python | xmltodict,一个非常厉害的 关于XML数据 Python 库!
    查看>>