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;
    }
}

0 0
原创粉丝点击