[Question]问题描述:

单独的ListView列表能自动垂直滚动,但当将ListView嵌套在ScrollView后,会和ScrollView的滚动滑块冲突,造成ListView滑块显示不完整。

activity_main.xml表现:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
****此处省略ScrollView的修饰代码***
> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:text="test"/> <ListView
android:id="@+id/list_class"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"/>
</LinearLayout> </ScrollView>

[Solution]解决方法:

第一步:自定义ListViewForScrollView类:  ListViewForScrollView.java

package com.jack.helloen.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView; /**
* 自定义一个类继承自ListView,通过重写其onMeasure方法,达到对ScrollView适配的效果。
*/
public class ListViewForScrollView extends ListView {
public ListViewForScrollView(Context context) {
super(context);
} public ListViewForScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
} public ListViewForScrollView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
} @Override
/**
* 重写该方法,达到使ListView适应ScrollView的效果
*/
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}

第二步: MainActivity.java里增加兼容patch

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);

//ListView兼容ScrollView
        sv = (ScrollView) findViewById(R.id.main_scroll);
        sv.smoothScrollTo(0, 0);
}

第三步:布局文件xml 里 引用自定义 组件

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/main_scroll"
*** >

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

<TextView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:text="test"/>

<com.jack.helloen.ui.ListViewForScrollView
            android:id="@+id/list_class"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="none"/>
    </LinearLayout>
</ScrollView>

本博客地址: wukong1688

本文原文地址:https://www.cnblogs.com/wukong1688/p/10657559.html

转载请著名出处!谢谢~~

[Android] Android最简单ScrollView和ListView滚动冲突解决方案的更多相关文章

  1. 【Android - 问题解决】之ScrollView嵌套ListView时总是自动滑动到ListView顶部的问题

    最近做了一个项目,里面有一个ScrollView嵌套ListView的布局. 做出来之后发现,进入这个界面之后,总是自动滑动到ListView的顶部,而ScrollView中位于ListView上面的 ...

  2. Android——MeasureSpec学习 - 解决ScrollView嵌套ListView和GridView冲突的方法

      原文地址:http://blog.csdn.net/yuhailong626/article/details/20639217   在自定义View和ViewGroup的时候,我们经常会遇到int ...

  3. 【朝花夕拾】Android自定义View篇之(七)Android事件分发机制(下)滑动冲突解决方案总结

    前言 转载请声明,转自[https://www.cnblogs.com/andy-songwei/p/11072989.html],谢谢! 前面两篇文章,花了很大篇幅讲解了Android的事件分发机制 ...

  4. ScrollView与ListView的冲突

    众所周知ListView与ScrollView都具有滚动能力,对于这样的View控件,当ScrollView与ListView相互嵌套会成为一种问题: 问题一:ScrollView与ListView嵌 ...

  5. 解决ScrollView与ListView事件冲突

    1,在最近做项目的时候使用ScrollView嵌套ListView的时候发现ListView的滑动效果失效,简单的网上搜索了一下,也就有了下面的解决方法,在ListView中设置事件的监听listvi ...

  6. Android布局中ScrollView与ListView的冲突的最简单方法

    看到网上流行的一种使用方法是: public class Utility { public static void setListViewHeightBasedOnChildren(ListView ...

  7. Android ScrollView与ListView的冲突解决办法汇总

    1. public  void setListViewHeight(){ ListAdapter listadapter = lv.getAdapter(); if (listadapter == n ...

  8. Android ScrollView和ListView滑动冲突解决记录

    private int mLastX; private int mLastY; public View.OnTouchListener onTouchListener = new View.OnTou ...

  9. Android View的事件分发机制和滑动冲突解决方案

    这篇文章会先讲Android中View的事件分发机制,然后再介绍Android滑动冲突的形成原因并给出解决方案.因水平有限,讲的不会太过深入,只希望各位看了之后对事件分发机制的流程有个大概的概念,并且 ...

随机推荐

  1. 「POJ - 2318」TOYS (叉乘)

    BUPT 2017 summer training (16) #2 A 题意 有一个玩具盒,被n个隔板分开成左到u右n+1个区域,然后给每个玩具的坐标,求每个区域有几个玩具. 题解 依次用叉积判断玩具 ...

  2. nagios 监控内存使用情况

    监控本机内存cd /usr/lib64/nagios/pluginstouch check_mem.sh #!/bin/bash " ]; then memTotal_b=`free -b ...

  3. 【COGS2652】秘术「天文密葬法」(长链剖分,分数规划)

    [COGS2652]秘术「天文密葬法」(长链剖分,分数规划) 题面 Cogs 上面废话真多,建议直接拉到最下面看一句话题意吧: 给个树,第i个点有两个权值ai和bi,现在求一条长度为m的路径,使得Σa ...

  4. 【Luogu3731】[HAOI2017]新型城市化(网络流,Tarjan)

    [Luogu3731][HAOI2017]新型城市化(网络流,Tarjan) 题面 洛谷 给定一张反图,保证原图能分成不超过两个团,问有多少种加上一条边的方法,使得最大团的个数至少加上\(1\). 题 ...

  5. Redhat上为java Maven项目构建基于Jenkins + Github的持续集成环境

    在Redhat enterprise 6.5 的服务器上,为在gutub 上的 java mvaen项目构建一个持续集成环境,用到了Jenkins.因公司的服务器在内网,访问外网时要通过代理,所以为m ...

  6. 【原创】hdu 1166 敌兵布阵(线段树→单点更新,区间查询)

    学习线段树的第三天...真的是没学点啥好的,又是一道水题,纯模板,我个人觉得我的线段树模板还是不错的(毕竟我第一天相当于啥都没学...找了一整天模板,对比了好几个,终于找到了自己喜欢的类型),中文题目 ...

  7. 用 Homebrew 带飞你的 Mac

    文章目录 资料 安装 基本用法 源镜像 Homebrew也称brew,macOS下基于命令行的最强大软件包管理工具,使用Ruby语言开发.类似于CentOS的yum或者Ubuntu的apt-get,b ...

  8. 回调函数: 一定要在函数名前加上 CALLBACK,否则有可能引起内存崩溃!

    今天又遇到一个莫名其妙的内存崩溃问题,问题代码 EnumChildWindows(...): EnumChildWindows(hwnd_panel_text_watermark, (WNDENUMP ...

  9. TCHAR和CHAR类型的互转,string 转lpcwstr

    https://www.cnblogs.com/yuguangyuan/p/5955959.html 没有定义UNICODE,所以它里面的字符串就是简单用" "就行了,创建工程的时 ...

  10. JavaScript(JS)之Javascript对象DOM(三)

    https://www.cnblogs.com/haiyan123/p/7598320.html 一.什么是HTML  DOM HTML  Document Object Model(文档对象模型) ...