废话少说,直接上例子~~~~~~~~~~~~~
1、先上效果图:
2、例子详解
main.xml布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#00ffff"
android:text="-----title--yyy----"
android:textColor="#ff0000" />
<com.example.android.scrolltricks.ObservableScrollView
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="@drawable/a3"
android:scaleType="matrix" />
<TextView
android:id="@+id/placeholder"
android:layout_width="match_parent"
android:layout_height="@dimen/sticky_height"
android:background="#00ffff"
android:text="place--------yyy----"
android:textColor="#ff0000" />
<TextView
android:textSize="100dp"
android:layout_width="match_parent"
android:layout_height="1000dp"
android:background="#ff00ff"
android:text="content" />
</LinearLayout>
<!-- -->
<LinearLayout
android:id="@+id/sticky"
android:layout_width="match_parent"
android:layout_height="@dimen/sticky_height"
android:background="#e0000000"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="-嘻嘻嘻嘻嘻嘻嘻嘻嘻惺惺惜惺惺--"
android:textColor="#ff0000" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="-------------嘻嘻嘻嘻嘻嘻嘻嘻惺惺惜惺------------xxx" />
</LinearLayout>
</FrameLayout>
</com.example.android.scrolltricks.ObservableScrollView>
</LinearLayout>
重写Scrollview:
public class ObservableScrollView extends ScrollView {
private Callbacks mCallbacks;
public ObservableScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (mCallbacks != null) {
mCallbacks.onScrollChanged(t);
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (mCallbacks != null) {
switch (ev.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
mCallbacks.onDownMotionEvent();
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
mCallbacks.onUpOrCancelMotionEvent();
break;
}
}
return super.onTouchEvent(ev);
}
@Override
public int computeVerticalScrollRange() {
return super.computeVerticalScrollRange();
}
public void setCallbacks(Callbacks listener) {
mCallbacks = listener;
}
public static interface Callbacks {
public void onScrollChanged(int scrollY);
public void onDownMotionEvent();
public void onUpOrCancelMotionEvent();
}
}
MainActivity.java 核心代码:
mStickyView = (LinearLayout) findViewById(R.id.sticky);
mPlaceholderView = rootView.findViewById(R.id.placeholder);
mObservableScrollView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
onScrollChanged(mObservableScrollView.getScrollY());
}
});
//该方法会布局变化时候调用,初始化后mStickyView将占有mPlaceholderView的位置
@Override
public void onScrollChanged(int scrollY) {
Log.i(tag, "scrollY-->"+scrollY+"; mPlaceholderView.getTop()-->"+mPlaceholderView.getTop());
mStickyView.setTranslationY(Math.max(mPlaceholderView.getTop(), scrollY));
}
ok,代码比较简单吧.............