Android ScrollView 和ListView 一起使用的问题汇总
1.ScrollView 嵌套 ListView ,touch事件的截获问题。
参考 http://www.cnblogs.com/lqminn/archive/2013/03/02/2940194.html
http://blog.csdn.net/chaihuasong/article/details/17499799
_scrollView.requestDisallowInterceptTouchEvent(true);
这句话的意思是告诉scrollView,滚动的事件交给我处理。用完以后记得还回去
_scrollView.requestDisallowInterceptTouchEvent(false);
如果不设置回去,ScrollView将无法滚动了。
2.ScrollView 滚动时,ListView的第一个条目是否处于显示状态?
参考 http://stackoverflow.com/questions/4628800/android-how-to-check-if-a-view-inside-of-scrollview-is-visible
boolean checkNeedRefresh() {
Rect scrollBounds = new Rect();
View firstChild = listView.getChildAt(0);
_scrollView.getHitRect(scrollBounds);
if (firstChild.getLocalVisibleRect(scrollBounds)) {
// Any portion of the firstChild, even a single pixel, is within the
// visible window
return true;
} else {
// NONE of the firstChild is within the visible window
return false;
}
}
3. listView不能显示完整
参考 http://blog.csdn.net/hahashui123/article/details/39177057
http://blog.csdn.net/solomonxiang/article/details/26507145
public static void setListViewHeight(ListView listView) {
try {
int totalHeight = 0;
ListAdapter adapter = listView.getAdapter();
for (int i = 0, len = adapter.getCount(); i < len; i++) { // listAdapter.getCount()
View listItem = adapter.getView(i, null, listView);
listItem.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listView.getCount() - 1));
listView.setLayoutParams(params);
} catch (Exception ex) {
ex.printStackTrace();
}
}
顺带GridView
public static void setGridViewHeight(GridView gridView, int numColumns) {
try {
ListAdapter adapter = gridView.getAdapter();
int row = 3;
View listItem = adapter.getView(0, null, gridView);
if (listItem == null)
return;
listItem.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
listItem.measure(0, 0);
int totalHeight = listItem.getMeasuredHeight() * row
+ (gridView.getVerticalSpacing() * (row - 1));
ViewGroup.LayoutParams params = gridView.getLayoutParams();
params.height = totalHeight;
gridView.setLayoutParams(params);
} catch (Exception ex) {
ex.printStackTrace();
}
}
如果ListView 带有BottomView
public static void setListViewHeight(ListView listView) {
try {
int totalHeight = 0;
int bottomHeight = 0;
ListAdapter dataAdapter = null;
int totalItems = 0;
ListAdapter adapter = listView.getAdapter();
if (adapter instanceof HeaderViewListAdapter) {
HeaderViewListAdapter headerViewListAdapter = ((HeaderViewListAdapter) adapter);
dataAdapter = headerViewListAdapter.getWrappedAdapter();
totalItems = dataAdapter.getCount();
int allItems = headerViewListAdapter.getCount();
View bottomItem = headerViewListAdapter.getView(allItems - 1,
null, listView);
bottomItem.measure(0, 0);
bottomHeight = bottomItem.getMeasuredHeight();
} else {
dataAdapter = adapter;
}
for (int i = 0, len = totalItems; i < len; i++) {
View listItem = dataAdapter.getView(i, null, listView);
listItem.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
int listviewCount = listView.getCount();
int height = totalHeight
+ (listView.getDividerHeight() * listviewCount + 1)
+ bottomHeight;
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = height;
listView.setLayoutParams(params);
listView.requestLayout();
} catch (Exception ex) {
ex.printStackTrace();
}
}
4. 其他,自定义控件实现ListView
http://www.cnblogs.com/lesliefang/p/3587154.html
5. 发现 每次加载完成后,listview总是滚到 屏幕最上方,实际上listview上面还有东西被盖住了,解决办法如下
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical" >
最关键的一句, 找到srcollview的 内容控件,一般是 LinearLayout,加上属性 android:descendantFocusability="blocksDescendants"
就可以了。
Android ScrollView 和ListView 一起使用的问题汇总的更多相关文章
- Android ScrollView与ListView的冲突解决办法汇总
1. public void setListViewHeight(){ ListAdapter listadapter = lv.getAdapter(); if (listadapter == n ...
- Android ScrollView 嵌套 ListView、 ListView 嵌套ScrollView Scroll事件冲突解决办法
本人菜鸟一名,最近工作了,开始学习Android. 最近在做项目的时候,UX给了个design,大概就是下拉刷新的ListView中嵌套了ScrollView,而且还要在ScrollView中添加动画 ...
- android scrollview嵌套listview计算高度的问题
ScrollView中只能放一个控件,一般都放LinearLayout,orientation属性值为vertical.在LinearLayout中放需要呈现的内容.ListView也在其中,List ...
- Android ScrollView和ListView联用,且ListView可以下拉刷新和上拉加载
ScrollView嵌套listView且ListView可以实现上拉加载. 由于代码太长,在此只提供实现思路: 先不说上拉加载的事,咱们先回想一下,ScrollView和LsitView联用,时的解 ...
- Android ScrollView 嵌套ListView的替代方案
概要:本例仅提供替代思路. 原需求:实现下图这个布局 要求:头部菜单固定,实现Viewpager.中间的按钮菜单,底部的listview一起能够上下滚动. 做法: 把Viewpager.中间的按钮菜单 ...
- android:ScrollView嵌套ListView的问题
在ScrollView中嵌套使用ListView,看起来ListView只会显示一行多一点,不能滑动.ListView的高度怎么改都有问题,与预期不符合.搜索了一些解决方案,我觉得最好不要用这样的设计 ...
- Android scrollview嵌套listview运行后最先显示出来的位置不在顶部而是中间问题
scrollview里面嵌套了一个listview ,通过设置一个方法设置了listview的高度 现在的情况就是进到这个界面的时候看到的不是最上面 而是中间 ,该问题的解决办法为: mScrollV ...
- Android ScrollView和ListView滑动冲突解决记录
private int mLastX; private int mLastY; public View.OnTouchListener onTouchListener = new View.OnTou ...
- android 有弹性的ScrollView 简单实现,与处理ScrollView和ListView,GridView之间的冲突
处理ScrollView和ListView,GridView之间的冲突, 最好的办法就是继承这两个类,重写他们的onMeasure方法即可: ListView: import android.widg ...
随机推荐
- Oracle 执行报错表空间或临时表空间不足,降低水位线方法
Oracle 执行语句插入临时表报表空间不足,在增加表空间到最大后依然报错,经分析应该为创建的临时表水位线未清除导致,查询降低水位线方法如下: 原文地址:http://blog.itpub.net/2 ...
- Git克隆部分文件
克隆部分文件html, body {overflow-x: initial !important;}.CodeMirror { height: auto; } .CodeMirror-scroll { ...
- plot sin 动态配置rc settings
plot sin 动态配置rc settings 坐标轴颜色 线的颜色 绘图前景色 Code #!/usr/bin/env python # -*- coding: utf-8 -*- import ...
- 【Unity笔记】用代码动态修改Animator状态机的状态
通常情况下,Animator修改状态机,是在Animator定义参数(变量),状态之间建立切换的条件(箭头),然后代码中修改参数(变量),实现状态之间的切换. 而另一种情况下,不需要预先准备定义参数( ...
- HashMap源码学习
HashMap就是将key做hash算法,然后将hash值映射到内存地址,直接取得key所对应的数据. 关于hash算法的原理知识在之前的博客中有讲到:哈希表之一初步原理了解. 在Java中的Hash ...
- Ubuntu 安装 vnc server
安装原因,因为需要有桌面操作, 服务器上配置 vnc 即可实现. 在 ubuntu 14.04 上已经实现. 安装先关软件 sudo apt-get update sudo apt-get insta ...
- About darwin OS
Darwin 是一种Unix-like操作系统,整合数种的技术,包含开 Darwin LOGO 放原始码的XNU核心,一种以微核心为基础的核心架构来实作Machkernel.操作系统的服务和userl ...
- USB2.0学习笔记连载(十四):USB驱动安装及固件程序的编写
在之前的博客中已经讲过,驱动程序最核心的两个文件,一个是xxx.sys文件,一个是xxx.inf文件,主机是寻找xxx.inf文件. 在下面的文件中有相关关于USB驱动的说明.对于用户来说,xxx.s ...
- Google ProtocolBuffers2.4.1应用说明(一)
1.概念 Protocol buffers是一个用来序列化结构化数据的技术,支持多种语言诸如C++.Java以及Python语言,可以使用该技术来持久化数据或者序列化成网络传输的数据.相比较一些其他的 ...
- Simsimi 小黄鸡机器人最新无限制接口api simsimi机器人接口api 微信公众号
一.什么是Simsimi? simsimi公司是提供智能服务,其中一个服务是simsimi聊天机器人服务,每天有超过百万的用户聊天,国内最大的搜索引擎——百度的产品siri使用的就是simsimi提供 ...