80,351
社区成员
发帖
与我相关
我的任务
分享
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.scrollview.MainActivity" >
<com.example.scrollview.ObservableScrollView
android:id="@+id/sv"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/sj" />
<TextView
android:id="@+id/yidong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:text="移动的标题" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/sj" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n其让\r\n" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="其让啦啦啦" />
</LinearLayout>
</com.example.scrollview.ObservableScrollView>
</LinearLayout>
package com.example.scrollview;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
int[] location = new int[2];
int[] locationSv = new int[2];
private TextView yidong;
private ObservableScrollView sv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sv = (ObservableScrollView) findViewById(R.id.sv);
yidong = (TextView) findViewById(R.id.yidong);
sv.setScrollViewListener(new ScrollViewListener() {
@Override
public void onScrollChanged(ObservableScrollView scrollView, int x, int y,
int oldx, int oldy) {
// TODO Auto-generated method stub
yidong.getLocationOnScreen(location);
sv.getLocationOnScreen(locationSv);
sv.top=(location[1]-locationSv[1]);
//sv.v=yidong;
}
});
}
}
package com.example.scrollview;
public interface ScrollViewListener {
void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);
}
package com.example.scrollview;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ScrollView;
public class ObservableScrollView extends ScrollView {
private ScrollViewListener scrollViewListener = null;
public int top=Integer.MAX_VALUE;
public View v;
public ObservableScrollView(Context context) {
super(context);
}
public ObservableScrollView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
public ObservableScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setScrollViewListener(ScrollViewListener scrollViewListener) {
this.scrollViewListener = scrollViewListener;
}
@Override
protected void dispatchDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.dispatchDraw(canvas);
if(top<=0 && v!=null){
int pLeft = getPaddingLeft();
int pTop = getPaddingTop();
canvas.translate(pLeft, pTop - top);
//smoothScrollTo(pLeft, pTop - top);
drawChild(canvas, v, getDrawingTime());
}
}
@Override
protected void onScrollChanged(int x, int y, int oldx, int oldy) {
super.onScrollChanged(x, y, oldx, oldy);
if (scrollViewListener != null) {
scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
}
}
}