(第三種,第四種簡單推薦使用)
在這里我要提出的是,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 false;return 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 plaincopy public 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的單擊事件