Android development step by step 65: Solves the conflict between ScrollView and ListView touch event onInterceptTouchEvent,
Recently, there is a requirement in the project. A page has a ScrollView, And the whole page can be rolled up. Then, a ListView is nested in the ScrollView, and the data in the ListView can also be slide up and down, theoretically, the ListView is wrapped in the ScrollView, And the TouchEvent must have been intercepted by the ScrollView. What can we do? I think many practices on the Internet say that the height of the entire ListView is calculated, this is not elegant. If there are too many ListView data, isn't the page too long? One idea is that when we slide the ListView area, ScrollView does not respond to the OnTouch event. You can click a point outside the ListView area to slide the entire page. Provides the core code.
Custom ScrollView
Package com. figo. study. view; import java. util. arrayList; import java. util. list; import android. content. context; import android. util. attributeSet; import android. view. motionEvent; import android. view. view; import android. widget. scrollView; public class MyScrollView extends ScrollView {private OnScrollListener onScrollListener; private List <View> views = new ArrayList <View> (); public MyScrollView (Context co Ntext) {this (context, null);} public MyScrollView (Context context, AttributeSet attrs) {this (context, attrs, 0);} public MyScrollView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle);} public void setOnScrollListener (OnScrollListener onScrollListener) {this. onScrollListener = onScrollListener;} @ Override protected void onScrollChanged (int l, int t, int ol Dl, int oldt) {super. onScrollChanged (l, t, oldl, oldt); if (onScrollListener! = Null) {onScrollListener. onScroll (t) ;}} public interface OnScrollListener {public void onScroll (int scrollY);} // whether to intercept the touch event @ Override public boolean onInterceptTouchEvent (MotionEvent ev) {if (views! = Null & checkAllViews (views, ev) {return false;} return super. onInterceptTouchEvent (ev);} public void addUnTouchableView (View view) {try {if (! Views. contains (view) {views. add (view) ;}} catch (Exception e) {if (e! = Null) {e. printStackTrace () ;}} public void delUnTouchableView (View view) {try {if (views. contains (view) {views. remove (view) ;}} catch (Exception e) {if (e! = Null) {e. printStackTrace () ;}} public void delAllUnTouchableView () {try {if (views. size ()> 0) {views. clear () ;}} catch (Exception e) {if (e! = Null) {e. printStackTrace () ;}} private boolean checkAllViews (List <View> views, MotionEvent event) {for (View view: views) {if (checkInLvArea (view, event )) {return true ;}} return false;} private boolean checkInLvArea (View v, MotionEvent event) {try {float x = event. getRawX (); float y = event. getRawY (); int [] locate = new int [2]; v. getLocationOnScreen (locate); int l = locate [0]; int r = L + v. getWidth (); int t = locate [1]; int B = t + v. getHeight (); if (l <x & x <r & t <y & y <B) {return true;} return false;} catch (Exception e) {if (e! = Null) {e. printStackTrace () ;}} return false ;}}
Tip:
MyScrollView scrollView = (MyScrollView) findViewById(R.id.scrollView); scrollView.setOnScrollListener(new OnScrollListener() { @Override public void onScroll(int scrollY) { int top = Math.max(scrollY, mLayoutHead.getTop()/2); mLayoutHeadNew.layout(0, top, mLayoutHeadNew.getWidth(), top + mLayoutHeadNew.getHeight()); if(scrollY>=canScrollHeight) {// mLayoutHead.setVisibility(View.GONE); mLayoutHeadNew.setVisibility(View.VISIBLE); }else {// mLayoutHead.setVisibility(View.VISIBLE); mLayoutHeadNew.setVisibility(View.GONE); } } });