android之ScrollView里嵌套ListView(都可滚动)
来源:互联网 发布:js 字符串 match 编辑:程序博客网 时间:2024/05/25 13:33
http://www.eoeandroid.com/thread-246995-1-1.html
其实实现原理很简单ScrollView有一个方法requestDisallowInterceptTouchEvent(boolean);
这个方法是设置是否交出ontouch权限的,如果让外层的scrollview.requestDisallowInterceptTouchEvent(false);那么外层的onTouch权限会失去,这样里面的listview就能
拿到ontouch权限了,listView也就能滚了。
问题是:权限只有一个,要支持两个view都能滚动。这个就有点难实现了吧..
其实这个一点也不难,当手指触到listview的时候,让外面的scrollview交出权限,当手指松开后,外面的scrollview重新获得权限。这样ok了。
且看代码实现:
重写一个InnerListView extends ListView
InnerListView.java
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
setParentScrollAble(false);//当手指触到listview的时候,让父ScrollView交出ontouch权限,也就是让父scrollview 停住不能滚动
LogManager.d("onInterceptTouchEvent down");
case MotionEvent.ACTION_MOVE:
LogManager.d("onInterceptTouchEvent move");
break;
case MotionEvent.ACTION_UP:
LogManager.d("onInterceptTouchEvent up");
case MotionEvent.ACTION_CANCEL:
LogManager.d("onInterceptTouchEvent cancel");
setParentScrollAble(true);//当手指松开时,让父ScrollView重新拿到onTouch权限
break;
default:
break;
}
return super.onInterceptTouchEvent(ev);
}
/**
* 是否把滚动事件交给父scrollview
*
* @param flag
*/
private void setParentScrollAble(boolean flag) {
parentScrollView.requestDisallowInterceptTouchEvent(!flag);//这里的parentScrollView就是listview外面的那个scrollview
}
有些设备上,依然不响应。则修改ScrollView。
public class ScrollScrollView extends ScrollView {
GetListViewPos getPos;
public ScrollScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
if(getPos != null){
int scrollY = getScrollY();
int listTop = getPos.getTop() - scrollY;
int listBtm = getPos.getBottom() - scrollY;
int listLeft = getPos.getLeft();
int listR = getPos.getRight();
int y = (int) ev.getY();
int x = (int) ev.getX();
if(x > listLeft && x < listR && y > listTop && y < listBtm){
requestDisallowInterceptTouchEvent(true);
return false;
}
}
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
break;
default:
break;
}
boolean retn = super.onInterceptTouchEvent(ev);
return retn;
}
public static interface GetListViewPos{
int getTop();
int getBottom();
int getLeft();
int getRight();
}
public void setGetListViewPos(GetListViewPos getListViewPos) {
getPos = getListViewPos;
}
}
- android之ScrollView里嵌套ListView(都可滚动)
- android之ScrollView里嵌套ListView
- android之ScrollView里嵌套ListView
- android之ScrollView里嵌套ListView
- android之ScrollView里嵌套ListView
- android之ScrollView里嵌套ListView
- android之ScrollView里嵌套ListView
- android之ScrollView里嵌套ListView
- android之ScrollView里嵌套ListView
- android之ScrollView里嵌套ListView
- android之ScrollView里嵌套ListView(转)
- android之ScrollView里嵌套ListView(转)
- android之ScrollView里嵌套ListView(转)
- android之ScrollView里嵌套ListView(转)
- android之ScrollView里嵌套ListView(转)
- android之ScrollView里嵌套ListView(转)
- android之ScrollView里嵌套ListView(转)
- android之ScrollView里嵌套ListView,gridview,scrollview
- Report View 2008 升级至Report View 2012
- ExecutorService对象的shutdown()和shutdownNow()的区别
- shell笔记
- 2015我想和Java聊聊之NIO读写文件
- 线性表
- android之ScrollView里嵌套ListView(都可滚动)
- LightOJ 1038 概率期望
- 单例模式(Singleton)详解
- CYC-非常炫酷的UIView动画-02
- leetcode-107-Binary Tree Level Order Traversal II
- 测试基础 测试用例
- 测试用例设计白皮书--等价类划分方法
- Ubuntu:su和su -(su - root)的区别
- Apache Log4j使用详细介绍