Synchronizing Two Horizontal Scroll Views In Android
Solution 1:
The error points to a null pointer in a constructor in your ObservableScrollView. Could you please post the constructor, as well as line 12 on its own?
One possible issue I am seeing with the way your XML and custom scroll view interact is that your custom scroll view is an inner class. See this page for how to declare an inner class custom component in XML, or move it to an external class. I usually put it in an external class, but if you wanted to keep it as an inner class it would be something like
<view
class="com.glen.apps.TeacherAidePro$ObservableScrollView"
.../>
I don't think this explains your null pointer exception though, so please post your ObservableScrollView class.
EDIT:
If you insist on doing everything in Java, here is a working example:
privateObservableScrollViewscrollView1=null;
privateObservableScrollViewscrollView2=null;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayoutparent=newLinearLayout(this);
parent.setOrientation(LinearLayout.HORIZONTAL);
parent.setLayoutParams(newLayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
parent.setWeightSum(2.0f);
scrollView1 = newObservableScrollView(this);
scrollView1.setLayoutParams(newLayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT, 1.0f));
scrollView2 = newObservableScrollView(this);
scrollView2.setLayoutParams(newLayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT, 1.0f));
scrollView1.setScrollViewListener(newScrollViewListener() {
publicvoidonScrollChanged(ObservableScrollView scrollView, int x,
int y, int oldx, int oldy) {
scrollView2.scrollTo(x, y);
}
});
scrollView2.setScrollViewListener(newScrollViewListener() {
publicvoidonScrollChanged(ObservableScrollView scrollView, int x,
int y, int oldx, int oldy) {
scrollView1.scrollTo(x, y);
}
});
TextViewtv1=newTextView(this);
tv1.setText("TEXT1TEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXT");
tv1.setTextSize(36.0f);
scrollView1.addView(tv1);
TextViewtv2=newTextView(this);
tv2.setText("TEXT2TEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXTTEXT");
tv2.setTextSize(36.0f);
scrollView2.addView(tv2);
parent.addView(scrollView1);
parent.addView(scrollView2);
parent.invalidate();
setContentView(parent);
}
Basically, this creates two scroll views side by side, with weights of 1.0 each, and puts them in a LinearLayout with total weight on the layout of 2.0, so they each get half the width.
However, I highly recommend getting used to XML as it is much, much easier (in my opinion) to create layouts. It is also easier to spot mistakes, and the nested form of XML makes it easier to read. Anyway, hope this clears things up.
Solution 2:
This is my solution further explanation in the code:
//This should be inside yout oncreate
syncScrolls(firstView, secondView;
syncScrolls(secondView, firstView);
//This method accept 2 horizontal scroll views, but you could improve it passing an array. privatevoidsyncScrolls(final HorizontalScrollView currentView, final HorizontalScrollView otherView) {
//This create the Gesture Listener where we override the methods we need
GestureDetector.SimpleOnGestureListenergestureListener=newGestureDetector.SimpleOnGestureListener() {
@OverridepublicbooleanonFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
//Here onfling just return true, this way doesnt happensreturntrue;
}
@OverridepublicbooleanonScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
//On scroll we sync the movement of the both views
otherView.scrollTo(currentView.getScrollX(), 0);
returnsuper.onScroll(e1, e2, distanceX, distanceY);
}
@OverridepublicbooleanonDown(MotionEvent e) {
returntrue;
}
};
//This create the gesture detectors that implements the previous custom gesture listenerfinalGestureDetectorgestureDetector=newGestureDetector(this, gestureListener);
//And finally we set everything to the views we need
currentView.setOnTouchListener(newView.OnTouchListener() {
publicbooleanonTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});
}
Im not sure this is a completely good answers, well as I mentioned before you can improve this by using an array, insted of calling the method for each view to sync, int his case just 2. Also onFling is prevented instead of being sync, if your scroll view is too long, this could not be user friendly. And finally, there is still a litle problem if the user does a tiny fling, but its a very rare case.
Post a Comment for "Synchronizing Two Horizontal Scroll Views In Android"