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

    你可能感兴趣的文章
    NMAP网络扫描工具的安装与使用
    查看>>
    NMF(非负矩阵分解)
    查看>>
    nmon_x86_64_centos7工具如何使用
    查看>>
    NN&DL4.1 Deep L-layer neural network简介
    查看>>
    NN&DL4.3 Getting your matrix dimensions right
    查看>>
    NN&DL4.7 Parameters vs Hyperparameters
    查看>>
    NN&DL4.8 What does this have to do with the brain?
    查看>>
    nnU-Net 终极指南
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    no available service ‘default‘ found, please make sure registry config corre seata
    查看>>
    No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>