Skip to content

Instantly share code, notes, and snippets.

@cutiko
Last active June 28, 2016 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cutiko/0b1e29ce4c14003c2da364cb555a583c to your computer and use it in GitHub Desktop.
Save cutiko/0b1e29ce4c14003c2da364cb555a583c to your computer and use it in GitHub Desktop.
Syncronized scroll views
<!--Im skipping here the mandatory xmlns atr to send this quick-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Table Head Container-->
<HorizontalScrollView
android:id="@+id/rightHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<!--Right Col Labels-->
<LinearLayout
android:id="@+id/labelsContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
<!--Body-->
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--General two cols container-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!--Human Resource Column-->
<LinearLayout
android:id="@+id/workforceList"
android:tag="leftWorkforceCol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
<!--Cards Column-->
<HorizontalScrollView
android:id="@+id/rightBody"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/workforceCards"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>
</LinearLayout>
//We will simply pass two ScrollViews to our method.
//This is not perfect cause you have to syncScrolls(first, second), and then syncScrolls(second, first)
//Please look at the xml below, the views to pass should be rightHeader, rightBody and viceversa
private void syncScrolls(final HorizontalScrollView currentView, final HorizontalScrollView otherView) {
GestureDetector.SimpleOnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDoubleTap(MotionEvent e) {
return true;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
//Onfling is very important to be considered, cause is the fast scroll, like when flip a marble with the finger
//Here we are ignoring it
return true;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
//Sorry again but this is not 100% perfect if the user is really annoying, like intentionally trying to fuck
//thinks up, the user could desync both views. Is a very rare case, and happens if the user
//move the finger back and forward (in the scroll direction) quickly in a 10mm range, is very, very uncommon
//the problem is, that gesture is not catch by Android!... go figure
otherView.scrollTo(currentView.getScrollX(), 0);
return super.onScroll(e1, e2, distanceX, distanceY);
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
};
//Here "this" is the context, use getApplicactionContext or getContext if needed
final GestureDetector gestureDetector = new GestureDetector(this, gestureListener);
currentView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment