Recently the project has a demand, a page put a scrollview, the entire page can scroll up, and then scrollview inside and nested a listview,listview inside the data can also swipe up and down, In theory the ListView is wrapped in ScrollView inside, touchevent must be ScrollView intercept, that we can take what train of thought, I see online a lot of practice said is the height of the entire ListView calculated, feel so very not elegant, If the ListView data is too large, then the page is not very long? One of the ideas is that when we slide the ListView area, ScrollView don't respond to the Ontouch event. Touch a point outside the ListView area to slide the entire page. Give 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 context) {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 oldl, 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 touch events @Override public boolean onintercepttouchevent (motionevent ev) {if (see! = 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:view s) {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; }}
after sliding the ScrollView, show the new page head tips:
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);}} );
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Development Step by Step 65: Resolve ScrollView and ListView touch Event onintercepttouchevent Conflicting issues