mytechead

Android & Wordpress Help center

Android: Gracefully handle nested scrollbars

Since the time I started android development, having nested scrollbars intercept their touch listeners for a smooth, even and intended scrolling has always been a pain in my ass. But thank god, finally I got the courage to face this and developed a listener which can handle nested scrollbars gracefully.

So here is the source code of my nested scroll listener class:

import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

/**
* Class enables touch events of a double nested scrollviews / scrollbars
*
* Usage: Set this touchlistener to childview of the nested scrollviews
*
* e.g.
*
* NestedScrollHelper nestedScrollHelper = new NestedScrollHelper();
*
* public boolean onTouch(View v, MotionEvent event){
* // Touch listener of child view
* // Do all the stuff you want to do in this touchlistener of child view
* // Additionally…..
* // Add the listener to handle nested scrollviews
* nestedScrollHelper.onTouch(v, event);
* return false;
* }
*
* @author shoaib
*
*/
public class NestedScrollHelper implements OnTouchListener{

@Override
public boolean onTouch(View v, MotionEvent event) {

// get the event action
int action = event.getAction();

// For each of the action DOWN/UP/MOVE
// disallow touch intercept to parent
switch (action) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_MOVE:
// Disallow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(true);
}

// Handle childview’s touch events.
v.onTouchEvent(event);
return true;
}
}

Now once you have this class, inside your Activity you can add this listener something like this:

public class MainActivity extends Activity implements OnTouchListener{

ListView myListView;
NestedScrollHelper nestedScrollHelper = new NestedScrollHelper();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

myListView = (ListView) findViewById(R.id.listview_id);
myListView.setOnTouchListener(this);

}

public boolean onTouch(View v, MotionEvent event){
// Touch listener of child view
// Do all the stuff you want to do in this touchlistener of child view
// Additionally…..
// Add the listener to handle nested scrollviews
nestedScrollHelper.onTouch(v, event);
return false;
}

}

This is it. You can handle the touch event how ever you want inside this onTouch method and by just calling the nestedScrollHelper.onTouch(v, event); inside this method [onTouch] scrolling will work perfectly smooth.

Hope it helps!

 

PS: Special thanks to my office colleague who helped me in this.

Advertisements

Single Post Navigation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: