This article describes the Android implementation of two ScrollView synchronized scrolling effect code. Share to everyone for your reference, specific as follows:
Recently in doing a project, using two scrollview the effect of interaction, simply, the effect of the linkage is to slide one of the ScrollView another ScrollView also along with the slide, to do with synchronized sliding. Feeling in the future development of the project may also be used, absolutely do a demo to share out, for everyone to study together, so that everyone can use later, feel good, useful to the first collection!
In fact, the scrollview,android authorities did not provide relevant methods to obtain or set the sliding distance, and did not provide a corresponding monitoring method. But how do you do that? Don't worry, you can certainly do it. Since there is no listening method, but by looking at the Android source, a method for x,y distance is provided, as follows:
Copy Code code as follows:
protected void onscrollchanged (int x, int y, int oldx, int oldy)
However, the method is protected and cannot be invoked directly, so we have to rewrite ScrollView and write an interface to listen for callbacks.
Effect Chart:
The implementation methods are as follows:
1. Rewrite ScrollView
Package Net.loonggg.scrollviewdemo;
Import Android.content.Context;
Import Android.util.AttributeSet;
Import Android.widget.ScrollView;
public class Observablescrollview extends ScrollView {
private scrollviewlistener scrollviewlistener = null;
Public Observablescrollview {
super (context);
}
Public Observablescrollview (context, AttributeSet attrs,
int defstyle) {
Super (context, attrs, Defstyle);
}
Public Observablescrollview (context, AttributeSet attrs) {
Super (context, attrs);
}
public void Setonscrollviewlistener (Scrollviewlistener scrollviewlistener) {
This.scrollviewlistener = Scrollviewlistener;
}
@Override
protected void onscrollchanged (int x, int y, int oldx, int oldy) {
super.onscrollchanged (x, y, OLDX, O Ldy);
if (Scrollviewlistener!= null) {
scrollviewlistener.onscrollchanged (this, x, Y, OLDX, Oldy);}}}
By using the top code, we can see that we have written a way to set up the listener, as follows:
public void Setonscrollviewlistener (Scrollviewlistener scrollviewlistener) {
This.scrollviewlistener = Scrollviewlistener;
}
So look at the second step, define a listener interface.
2. Define the Listener interface
Package Net.loonggg.scrollviewdemo;
Public interface Scrollviewlistener {
void onscrollchanged (Observablescrollview scrollview, int x, int y,
int OLDX, int oldy);
3. Observablescrollview of the layout file reference rewrite
The specific content is not written, for example can be cited as follows:
<net.loonggg.scrollviewdemo.observablescrollview
android:id= "@+id/sv1"
android:layout_width= "fill _parent "
android:layout_height=" fill_parent "
android:layout_weight=" 1.0 "
android:background=" # 36b797 ">
//Inside put package content
</net.loonggg.scrollviewdemo.ObservableScrollView>
4. Call in activity
Package Net.loonggg.scrollviewdemo;
Import Android.os.Bundle;
Import Android.view.Window;
Import android.app.Activity;
public class Mainactivity extends activity implements Scrollviewlistener {
private observablescrollview sv1, Sv2;
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Requestwindowfeature (window.feature_no_title);
Setcontentview (r.layout.activity_main);
SV1 = (Observablescrollview) Findviewbyid (R.ID.SV1);
Sv2 = (Observablescrollview) Findviewbyid (R.ID.SV2);
Sv1.setonscrollviewlistener (this);
Sv2.setonscrollviewlistener (this);
}
@Override public
void onscrollchanged (Observablescrollview scrollview, int x, int y,
int oldx, int oldy) {
if (ScrollView = = sv1) {
Sv2.scrollto (x, y);
} else if (ScrollView = sv2) {
Sv1.scrollto (x, y);
}
}
}
Here is finished, should say more understand it.
I hope this article will help you with the Android program.