关于ScrollView中嵌套listview焦点滑动问题 解决

来源:互联网 发布:seo是什么职位 编辑:程序博客网 时间:2024/06/11 05:25
(第三种,第四种简单推荐使用)

在这里我要提出的是,listview能滚动的前提是:当listview本身的高度小于listview里的子view。

第一种方法

只需在MainActivity中 找到listview 和 scrollview

然后给listview设置监听事件

listView.setOnTouchListener(new OnTouchListener() {                        public boolean onTouch(View v, MotionEvent event) {                // TODO Auto-generated method stub                              if(event.getAction() == MotionEvent.ACTION_UP){                      scrollView.requestDisallowInterceptTouchEvent(false);                  }else{                      scrollView.requestDisallowInterceptTouchEvent(true);                  }                  return false;            }        });

第二种方法

只需重写listview即可

package com.bawei.day06;import android.content.Context;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.widget.ListView;public class ListViewForScrollView extends ListView {    int mLastMotionY;    boolean bottomFlag;    public ListViewForScrollView(Context context, AttributeSet attrs) {        super(context, attrs);        // TODO Auto-generated constructor stub    }        @Override    public boolean onInterceptTouchEvent(MotionEvent ev) {                if (bottomFlag) {            getParent().requestDisallowInterceptTouchEvent(true);        }        return super.onInterceptTouchEvent(ev);    }    @Override    public boolean onTouchEvent(MotionEvent ev) {        // TODO Auto-generated method stub        int y = (int) ev.getRawY();        switch (ev.getAction()) {        case MotionEvent.ACTION_DOWN:            mLastMotionY = y;            break;        case MotionEvent.ACTION_MOVE:            int deltaY = y - mLastMotionY;            if (deltaY < 0) {                View child = getChildAt(0);                if (child != null) {                    if (getLastVisiblePosition() == (getChildCount()-1) && child.getBottom() == (getChildCount()-1)) {                        bottomFlag = true;                        getParent().requestDisallowInterceptTouchEvent(true);                    }                    int bottom = child.getBottom();                    int padding = getPaddingTop();                    if (getLastVisiblePosition() == (getChildCount()-1)                            && Math.abs(bottom - padding) >= 20) {                        bottomFlag = true;                        getParent().requestDisallowInterceptTouchEvent(true);                    }                }            }            break;        case MotionEvent.ACTION_UP:        case MotionEvent.ACTION_CANCEL:            break;        }        return super.onTouchEvent(ev);    }    public void setBottomFlag(boolean flag) {        bottomFlag = flag;    }}

 

第三种方法

只需重写listview即可

package com.bawei.day06;import android.content.Context;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.widget.ListView;public class ListViewForScrollView extends ListView {    int mLastMotionY;    boolean bottomFlag;    public ListViewForScrollView(Context context, AttributeSet attrs) {        super(context, attrs);        // TODO Auto-generated constructor stub    }    @Override    public boolean dispatchTouchEvent(MotionEvent ev) {        // TODO Auto-generated method stub        getParent().requestDisallowInterceptTouchEvent(true);                return  super.dispatchTouchEvent(ev);    }}

 

第四种方法

只需在MainActivity中 找到listview 

然后给listview设置监听事件

listView.setOnTouchListener(new OnTouchListener() {                        public boolean onTouch(View v, MotionEvent event) {                // TODO Auto-generated method stub                listView.getParent().requestDisallowInterceptTouchEvent(true);                return false;            }        });

 解释原理:

当手指触摸到屏幕时,系统就会调用相应View的onTouchEvent,并传入一系列的action。当有多个层级的View时,在父层级允许的情况下,这个action会一直向下传递直到遇到最深层的View。所以touch事件最先调用的是最底层View的onTouchEent,如果View的onTouchEvent接收到某个touch action并作了相应处理,最后有两种返回方式return true和return falsereturn true会告诉系统当前的View需要处理这次的touch事件,以后的系统发出的ACTION_MOVE,ACTION_UP还是需要继续监听并接收的,而且这次的action已经被处理掉了,父层的View是不可能出发onTouchEvent了。所以每一个action最多只能有一个onTouchEvent接口返回true。如果return false,便会通知系统,当前View不关心这一次的touch事件,此时这个action会传向父级,调用父级View的onTouchEvent。但是这一次的touch事件之后发出的任何action,该View都不会再接受,onTouchEvent在这一次的touch事件中再也不会触发,也就是说一旦View返回false,那么之后的ACTION_MOVE,ACTION_UP等ACTION就不会在传入这个View,但是下一次touch事件的action还是会传进来的。    前面说了底层的View能够接收到这次的事件有一个前提条件:在父层级允许的情况下。假设不改变父层级的dispatch方法,在系统调用底层onTouchEvent之前会先调用父View的onInterceptTouchEvent方法判断,父层View是不是要截获本次touch事件之后的action。如果onInterceptTouchEvent返回了true,那么本次touch事件之后的所有action都不会再向深层的View传递,统统都会传给父层View的onTouchEvent,就是说父层已经截获了这次touch事件,之后的action也不必询问onInterceptTouchEvent,在这次的touch事件之后发出的action时onInterceptTouchEvent不会再次调用,直到下一次touch事件的来临。如果onInterceptTouchEvent返回false,那么本次action将发送给更深层的View,并且之后的每一次action都会询问父层的onInterceptTouchEvent需不需要截获本次touch事件。只有ViewGroup才有onInterceptTouchEvent方法,因为一个普通的View肯定是位于最深层的View,touch事件能够传到这里已经是最后一站了,肯定会调用View的onTouchEvent。对于底层的View来说,有一种方法可以阻止父层的View截获touch事件,就是调用getParent().requestDisallowInterceptTouchEvent(true);方法。一旦底层View收到touch的action后调用这个方法那么父层View就不会再调用onInterceptTouchEvent了,也无法截获以后的action。用例子总结一下onInterceptTouchEvent和onTouchEvent的调用顺序:假设最高层View叫OuterLayout,中间层View叫InnerLayout,最底层View叫MyVIew。调用顺序是这样的(假设各个函数返回的都是false)OuterLayout.onInterceptTouchEvent->InnerLayout.onInterceptTouchEvent->MyView.onTouchEvent->InnerLayout.onTouchEvent->OuterLayout.onTouchEvent。[java] view plaincopy@Override        public boolean dispatchTouchEvent(MotionEvent ev) {           getParent().requestDisallowInterceptTouchEvent(true);          return super.dispatchTouchEvent(ev);        }  这句话是告诉父view,我的事件自己处理 [java] view plaincopypublic boolean onTouch(View v, MotionEvent event) {       switch (event.getAction()) {       case MotionEvent.ACTION_MOVE:            pager.requestDisallowInterceptTouchEvent(true);           break;       case MotionEvent.ACTION_UP:       case MotionEvent.ACTION_CANCEL:           pager.requestDisallowInterceptTouchEvent(false);           break;       }  }   也可以写成类似于上面那样,当用户按下的时候,我们告诉父组件,不要拦截我的事件(这个时候子组件是可以正常响应事件的),拿起之后就会告诉父组件可以阻止。 还有一个关于子控件和父控件的事件响应问题 当父控件中有子控件的时候,并且父控件和子空间都有事件处理(比如单击事件)。这时,点击子控件,父控件的单击事件就无效了。比如一个LinearLayout里面有一个子控件TextView,但是TextView的大小没有LinearLayout大①如果LinearLayout和TextView都设置了单击事件,那么点击TextView区域的时候,触发的是TextView的事件,点击TextView以外的区域的时候,还是触发的LinearLayout的事件。②如果LinearLayout设置了单击事件,而TextView没有设置单击事件的话,那么不管单击的是TextView区域,还是TextView以外的区域,都是触发的LinearLayout的单击事件如果LinearLayout的大小和TextView一样的话,那么①如果LinearLayout和TextView都设置了单击事件,那么只有TextView的单击事件有效②如果LinearLayout设置了单击事件,而TextView没有设置单击事件的话,那么触发的是LinearLayout的单击事件

 

0 0
原创粉丝点击