[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. 「UVA10766」Organising the Organisation(生成树计数)

    BUPT 2017 Summer Training (for 16) #6C 题意 n个点,完全图减去m条边,求生成树个数. 题解 注意可能会给重边. 然后就是生成树计数了. 代码 #include ...

  2. [NOIp2008] 双栈排序 (二分图染色 + 贪心)

    题意 给你一个长为 \(n\) 的序列 \(p\) ,问是否能够通过对于两个栈进行 push, pop(print) 操作使得最后输出序列单调递增(即为 \(1 \cdots n\) ),如果无解输出 ...

  3. pandas to_html

    如何将表格数据以图片的形式展现,主要目的则是为了防止爬虫. 为了解决这个问题,刚开始选择的是matplotlib.pyplot.table,但由于随着数据的字段长短不一,且matplotlib实际落地 ...

  4. Kafka史上最详细原理总结

    https://blog.csdn.net/ychenfeng/article/details/74980531 Kafka Kafka是最初由Linkedin公司开发,是一个分布式.支持分区的(pa ...

  5. yd的汇总

    因为是我这只蒟蒻个人的汇总嘛,可能有些奇♂怪的东西或者不规范的语言出现啦,见谅见谅 搬了一些到知识汇总里,删了一些过时和无用的,少了好多=.= 1.STL_queue 经实践验证,!qs.empty( ...

  6. P3486 [POI2009]KON-Ticket Inspector

    啊!这题做的真是爽!除了DP这个方法是有提示的之外,这题居然没有题解,哈哈哈嘿嘿嘿.很自豪的说:全是我自己独立解出来的一道题,包括设计状态,推倒(☺)转移方程,最后记录路径. 好了,首先,我们发现这题 ...

  7. WebClient请求接口,get和post方法

    1,get方式 string URI = "url"; //实例化 WebClient client = new WebClient(); // client.UseDefault ...

  8. windows中用bat脚本更改环境变量

    机房同传了新的系统,不使用dev的话每次开机都要重新更改环境变量(其实也可以在编译命令里添加绝对路径).所以就去学习了一下用bat脚本更改path.以便每次开机可以一键更改添加环境变量 wmic en ...

  9. configure - 源代码安装的第一步

    configure是源代码安装的第一步,主要的作用是对即将安装的软件进行配置,检查当前的环境是否满足要安装软件的依赖关系 configure有许多参数可配,具体参见./configure --help ...

  10. crond守护进程实现定时监控某进程占有内存的大小

    1)添加计划任务 crontab -e会使用某个编辑器打开某个文件,然后在内输入需要执行的计划任务,保存后在/var/spool/cron/crontabs/下会出现以用户名命名的文件 2)计划任务如 ...