ScrollView与ListView的冲突
众所周知ListView与ScrollView都具有滚动能力,对于这样的View控件,当ScrollView与ListView相互嵌套会成为一种问题:
问题一:ScrollView与ListView嵌套导致ListView显示不全面
问题二:ScrollView不能正常滑动
解决方式一:
ScrollView+LinearLayout+ListView可以换成ScrollView+LinearLayout+LinearLayout,对于开发中,ScrollView所能滚动的样式形式各异,另外的话,ScrollView所显示的内容肯定不会太多,因此这种方案是合理而且可选的
解决方式二:
同样是替换:ListView具有HeaderView与FooterView2部分,因此,在非下拉刷新,上拉加载的需求中,完全可以使用ListView来代替ScrollView,因此是合理可选的方案
<ScrollView android:id="@+id/ScrollView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ffffffff" android:scrollbars="vertical" > <LinearLayout android:id="@+id/tmall" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="文本备忘" android:textSize="28sp" /> <ListView android:id="@+id/listview0" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="智能备忘" android:textSize="28sp" /> <ListView android:id="@+id/listview1" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> </ScrollView>
private void initView() { textListView = (ListView) findViewById(R.id.listview0); intelListView = (ListView) findViewById(R.id.listview1); textListView.setAdapter(new MyTestAdapter(this)); intelListView.setAdapter(new MyTestAdapter(this)); new ListViewUtils().setListViewHeightBasedOnChildren(textListView); new ListViewUtils().setListViewHeightBasedOnChildren(intelListView); }
解决方式三:
通过给ListView设置LayoutParams属性来改变。
主动计算和设置ListView的高度,这样的结果最终得出类似解决方案一效果,具体来说缺点是大材小用,但也是合理的解决办法。
public class ListViewUtils { public void setListViewHeightBasedOnChildren(ListView listView) { // 获取ListView对应的Adapter ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { return; } int totalHeight = 0; for (int i = 0, len = listAdapter.getCount(); i < len; i++) { // listAdapter.getCount()返回数据项的数目 View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); // 计算子项View 的宽高 totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度 } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); // listView.getDividerHeight()获取子项间分隔符占用的高度 // params.height最后得到整个ListView完整显示需要的高度 listView.setLayoutParams(params); } }
ScrollView与ListView的冲突的更多相关文章
- [Android] Android最简单ScrollView和ListView滚动冲突解决方案
[Question]问题描述: 单独的ListView列表能自动垂直滚动,但当将ListView嵌套在ScrollView后,会和ScrollView的滚动滑块冲突,造成ListView滑块显示不完整 ...
- 解决ScrollView与ListView事件冲突
1,在最近做项目的时候使用ScrollView嵌套ListView的时候发现ListView的滑动效果失效,简单的网上搜索了一下,也就有了下面的解决方法,在ListView中设置事件的监听listvi ...
- 关于ScrollView和listview的冲突关于的滑动和宽度
listview和ScrollView嵌套有两个冲突,关于listview显示不全的问题和listview和scrollview的滑动冲突 自定义listview package com.exmple ...
- ScrollView和ListView的冲突问题
在ScrollView添加一个ListView会导致listview控件显示不全,这是因为两个控件的滚动事件冲突导致.所以需要通过listview中的item数量去计算listview的显示高度,从而 ...
- ScrollView 与ListView 滑动冲突完美解决
一.介绍ListView高度的设置方法 二.根据实际需求解决冲突问题 一.介绍ListView高度的设置方法 在ScrollView中使用ListView,ListView的高度会不正常. 方式一:在 ...
- Android ScrollView与ListView的冲突解决办法汇总
1. public void setListViewHeight(){ ListAdapter listadapter = lv.getAdapter(); if (listadapter == n ...
- ScrollView和listview的冲突问题,关于宽度,和滑动
只需要重新listview即可 package com.exmple.listscrow; import java.util.logging.LogManager; import android.co ...
- Android布局中ScrollView与ListView的冲突的最简单方法
看到网上流行的一种使用方法是: public class Utility { public static void setListViewHeightBasedOnChildren(ListView ...
- ScrollView和ListView滑动冲突问题
1.在ScrollView里面嵌套ListView时,ListView的滑动事件无法响应. 先看下事件分发的过程: 由父View层的 onInterceptTouchEvent 到中间层的on ...
随机推荐
- jQuery插件 -- Form表单插件jquery.form.js
http://blog.csdn.net/zzq58157383/article/details/7718956 http://my.oschina.net/i33/blog/77250
- WinFrom 只启动一个exe,并且获得焦点
只启动一个exe方法: using System; using System.Collections.Generic; using System.Runtime.InteropServices; us ...
- 标准库errno.h 查看错误代码编号,errno:4 与error:2
log 里报错,errno:4 与errno:2 查了一下 errno.h --------下文来自百度百科 errno 编辑 errno 是记录系统的最后一次错误代码.代码是一个int型的值 ...
- HDU4891_The Great Pan_字符串水题
2014多校第五题,当时题面上的10^5写成105,我们大家都wa了几发,改正后我和一血就差几秒…不能忍 题目:http://acm.hdu.edu.cn/showproblem.php?pid=48 ...
- 通用js类库
/* 其它通用函数 */$(function() { // var General = function() { var _self = this; /* 写 cookie 操作 */ _self.S ...
- notepad正则表达式
文件名称匹配 文件名称: boost_chrono-vc100-mt-1_49.dll 对应的notepad正则表达式: \w*_\w*-\w*-\w*-\w*-\w*.dll 移除空行 查找目标: ...
- spring+hibernate常见异常集合
spring+hibernate出错小结: (1)java.lang.NoClassDefFoundError: org/hibernate/context/CurrentSessionContext ...
- OJ 1188 全排列---康托展开
题目描述 求n的从小到大第m个全排列(n≤20). 输入 n和m 输出 输出第m个全排列,两个数之间有一空格. 样例输入 3 2 样例输出 1 3 2 #include<cstdio> # ...
- &&队友最近一周水水
100130 练习5 5 hr ago 15.2 days Private qwerqqq 100093 DP2 16 hr ago 50.2 days Private qwerqqq 100092 ...
- shell实现trim函数-去除字符串两侧的空格(包括tab,space键)
shell实现trim函数效果去除字符串两侧的空格,以下三个命令等价,都能实现 sed 's/^\s*//' totrim.txt |sed 's/\s*$//'>trimed.txtsed ' ...