冲突--ListView与ScrollView冲突的4种解决方案
众所周知ListView与ScrollView都具有滚动能力,对于这样的View控件,当ScrollView与ListView相互嵌套会成为一种问题:
问题一:ScrollView与ListView嵌套导致ListView显示不全面
问题二:ScrollView不能正常滑动
解决方式一:
ScrollView+LinearLayout+ListView可以换成ScrollView+LinearLayout+LinearLayout,对于开发中,ScrollView所能滚动的样式形式各异,另外的话,ScrollView所显示的内容肯定不会太多,因此这种方案是合理而且可选的
解决方式二:
同样是替换:ListView具有HeaderView与FooterView2部分,因此,在非下拉刷新,上拉加载的需求中,完全可以使用ListView来代替ScrollView,因此是合理可选的方案
解决方式三:
主动计算和设置ListView的高度,这样的结果最终得出类似解决方案一效果,具体来说缺点是大材小用,但也是合理的解决办法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public class Utility { public static void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null ) { return ; } 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(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1 )); listView.setLayoutParams(params); } } |
解决方式四:
复写ScrollView,从事件方向进行处理,缺点是灵活性不够好
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
public class ListScrollView extends ScrollView { private List list = new ArrayList(); private int scrollPaddingTop; // scrollview的顶部内边距 private int scrollPaddingLeft; // scrollview的左侧内边距 private int [] scrollLoaction = new int [ 2 ]; // scrollview在窗口中的位置 private final static int UPGLIDE = 0 ; private final static int DOWNGLIDE = 1 ; private int glideState; public ListScrollView(Context context, AttributeSet attrs) { super (context, attrs); } private int downY = 0 ; private int moveY = 0 ; @Override public boolean dispatchTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: downY = ( int ) ev.getY(); //System.out.println("actiondown" + ev.getY()); break ; case MotionEvent.ACTION_MOVE: moveY= ( int ) ev.getY(); //System.out.println("move" + moveY + "down" + downY); if ((moveY - downY) >= 0 ) { //System.out.println("'''''''''DOWNGLIDE'''''''''''"); glideState = DOWNGLIDE; } else { //System.out.println("'''''''''UPGLIDE'''''''''''"); glideState = UPGLIDE; } break ; case MotionEvent.ACTION_UP: default : break ; } return super .dispatchTouchEvent(ev); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { // 该事件的xy是以scrollview的左上角为00点而不是以窗口为00点 int x = ( int ) ev.getX() + scrollLoaction[ 0 ]; int y = ( int ) ev.getY() + scrollLoaction[ 1 ]; for ( int i = 0 ; i < list.size(); i++) { ListView listView = list.get(i); int [] location = new int [ 2 ]; listView.getLocationInWindow(location); int width = listView.getWidth(); int height = listView.getHeight(); // 在listview的位置之内则可以滑动 if (x >= location[ 0 ] + scrollPaddingLeft && x <= location[ 0 ] + scrollPaddingLeft + width && y >= location[ 1 ] + scrollPaddingTop && y <= location[ 1 ] + scrollPaddingTop + height) { //System.out.println(glideState); if (( (listView.getLastVisiblePosition() == (listView.getCount()- 1 )) && (glideState == UPGLIDE) ) ) { //System.out.println("up"); break ; } if (( (listView.getFirstVisiblePosition() == 0 ) && (glideState == DOWNGLIDE))) { //System.out.println("down"); break ; } return false ; //让子控件直接处理 } } return super .onInterceptTouchEvent(ev); } @Override public boolean onTouchEvent(MotionEvent ev) { return super .onTouchEvent(ev); } private void findAllListView(View view) { if (view instanceof ViewGroup) { int count = ((ViewGroup) view).getChildCount(); for ( int i = 0 ; i < count; i++) { if (!(view instanceof ListView)) { findAllListView(((ViewGroup) view).getChildAt(i)); } } if (view instanceof ListView) { list.add((ListView) view); } } } @Override protected void onDraw(Canvas canvas) { super .onDraw(canvas); scrollPaddingTop = getTop(); scrollPaddingLeft = getLeft(); getLocationInWindow(scrollLoaction); } @Override protected void onLayout( boolean changed, int l, int t, int r, int b) { super .onLayout(changed, l, t, r, b); if ( this .getChildCount() != 1 ) { try { throw new ScrollException(); } catch (ScrollException e) { e.printStackTrace(); } } list.clear(); findAllListView( this .getChildAt( 0 )); } } |
冲突--ListView与ScrollView冲突的4种解决方案的更多相关文章
- ListView与ScrollView冲突的4种解决方案
问题解决方案1.手动设置ListView高度 经过测试发现,在xml中直接指定ListView的高度,是可以解决这个问题的,但是ListView中的数据是可变的,实际高度还需要实际测量.于是手动 ...
- (转载)ListView与ScrollView冲突的4种解决方案
问题解决方案1.手动设置ListView高度 经过测试发现,在xml中直接指定ListView的高度,是可以解决这个问题的,但是ListView中的数据是可变的,实际高度还需要实际测量.于是手动 ...
- 简单解决ListView和ScrollView冲突,复杂情况仅供参考
ScrollView嵌套ListView冲突问题的最优解决方案 (转) 记录学习之用 项目做多了之后,会发现其实 ScrollView嵌套ListVew或者GridView等很常用,但是你也会发现各种 ...
- ListView和ScrollView冲突
当ListView放在ScrollView中的时候,无论你设置高度为 match_parent(填充父窗体)和wrap_content(包裹内容)都只显示一行,这是你把ListView放在Linear ...
- Android ListView 和 ScrollView 冲突问题
近期做一款APP,当中有一个类似微博的评论功能的界面,先是列出微博的正文内容和图片等.然后下边是评论. 一開始就想着用一个ScrollView把主要内容和评论区的ListView包起来.然后加入各个控 ...
- Android——MeasureSpec学习 - 解决ScrollView嵌套ListView和GridView冲突的方法
原文地址:http://blog.csdn.net/yuhailong626/article/details/20639217 在自定义View和ViewGroup的时候,我们经常会遇到int ...
- android 开发-ListView与ScrollView事件冲突处理(事件分发机制处理)
ListView和ScrollView都存在滚动的效果,所以一般不建议listView和scrollView进行嵌套使用,但有些需求则需要用到两者嵌套.在android的学习中学了一种事件分发处理机制 ...
- 解决Android ListView 和 ScrollView 共存时冲突 问题 方法其一
转载请注明出处: http://www.goteny.com/articles/2013/11/8.html http://www.cnblogs.com/zjjne/p/3428480.html 当 ...
- 解决水平ListView在ScrollView中出现的滑动冲突
解决的问题有两个: 1)实现水平滑动的ListView.重写AdapterView,上代码: package com.liucanwen.horizontallistview.view; imp ...
随机推荐
- GNU C 扩展(转)
GNU CC 是一个功能非常强大的跨平台 C 编译器,它对 C 语言提供了很多扩展,这些扩展对优化.目标代码布局.更安全的检查等方面提供了很强的支持.这里对支持支持 GNU 扩展的 C 语言成为 GN ...
- 嵌入式Linux开发——内容介绍与开发环境的搭建
嵌入式Linux开发步骤 设计自己的硬件系统 编写Bootloader 裁剪自己的Linux内核 开发移植设备驱动 构建根文件系统 开发应用程序 嵌入式Linux学习要点 熟练使用开发工具和相关指令集 ...
- c++时间处理
struct tm;这是一个结构体,包括了时间的各个属性年月日,时分秒 time(time_t * t);获取从1900年到现在经过的毫秒数,或者也可以这么用time_t t=time(NULL); ...
- tomcat集群待整理
对于tomcat的集群有两种方式,这个主要是针对 session而言的.一种就是sticky模式,即黏性会话模式:另外一种就是session复制模式了.所谓sticky模式就是说同一个用户的访问 请求 ...
- HTML5 拖放(Drag 和 Drop)
拖放是一种常见的特性,即抓取对象以后拖到另一个位置. 在 HTML5 中,拖放是标准的一部分,任何元素都能够拖放. <!DOCTYPE HTML> <html> <hea ...
- 利用KMeans聚类进行航空公司客户价值分析
准确的客户分类的结果是企业优化营销资源的重要依据,本文利用了航空公司的部分数据,利用Kmeans聚类方法,对航空公司的客户进行了分类,来识别出不同的客户群体,从来发现有用的客户,从而对不同价值的客户类 ...
- EasyUI之DataGrid使用
http://blog.csdn.net/liovey/article/details/9173931 背景介绍: 原 先项目采用普通的jsp页面来做为前端显示,用户体验差,并且为了实现某一种效果需要 ...
- 使用SQL Server存储ASP.NET Session变量
创建和配置ASP.NET Session状态数据库 在基于NLB(网络负载平衡)环境下的ASP.NET Web应用程序开发,我们需要将Session存储在数据库中供多个Web应用程序调用,以下为配置方 ...
- oracle进程
http://blog.csdn.net/leshami/article/details/5529239 Oracle实例和Oracle数据库(Oracle体系结构) 几类进程:用户进程,服务进程,后 ...
- ORA-01013:用户请求取消当前的操作
ORA-01013:用户请求取消当前的操作 在测试一个通过ODBC连接ORACLE数据库的VB程序时,总是出现该错误,估计应该是数据量比较大,导致超时. 查到解决方法有如下四种 (选任意一种即可): ...