ScrollTricks是一个开源控件,实现了两个简单功能:
1、Quick Return:向上滑动时,View也向上滑动并且消失,当向下滑动时,View马上出现。例如Google Now的搜索功能。
2、Sticky:类似的同步滚动,特定的View最多滑动到顶部并保持固定不动。例如大众点评或美团的“立即购买”功能。
<com.example.android.scrolltricks.ObservableScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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">
<View style="@style/Item.Top" />
<View android:id="@+id/placeholder"
android:layout_width="match_parent"
android:layout_height="@dimen/sticky_height" />
<View style="@style/Item.Bottom" />
<View style="@style/Item.Bottom.Alt" />
<View style="@style/Item.Bottom" />
<View style="@style/Item.Bottom.Alt" />
<View style="@style/Item.Bottom" />
<View style="@style/Item.Bottom.Alt" />
</LinearLayout>
<TextView android:id="@+id/sticky" style="@style/Item.Sticky" />
</FrameLayout>
</com.example.android.scrolltricks.ObservableScrollView>
ScrollTricks的两个效果原理是两个相同的View同在一个FrameLayout布局,这里是android:id="@+id/placeholder",android:id="@+id/sticky"两个View。监控ScrollView的滑动,根据android:id="@+id/placeholder" View的位置控制android:id="@+id/sticky"View的位置。主要是对ScrollView滚动的Y值得监听。
看一下sticky的实现:
public class StickyFragment extends Fragment implements ObservableScrollView.Callbacks {
private TextView mStickyView;
private View mPlaceholderView;
private ObservableScrollView mObservableScrollView;
public StickyFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater
.inflate(R.layout.fragment_content, container, false);
mObservableScrollView = (ObservableScrollView) rootView.findViewById(R.id.scroll_view);
mObservableScrollView.setCallbacks(this);
mStickyView = (TextView) rootView.findViewById(R.id.sticky);
mStickyView.setText(R.string.sticky_item);
mPlaceholderView = rootView.findViewById(R.id.placeholder);
mObservableScrollView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
onScrollChanged(mObservableScrollView.getScrollY());
}
});
return rootView;
}
@Override
public void onScrollChanged(int scrollY) {
Log.d("onScroll", "Y:"+scrollY+"|"+mPlaceholderView.getTop());
mStickyView.setTranslationY(Math.max(mPlaceholderView.getTop(), scrollY));
}
@Override
public void onDownMotionEvent() {
}
@Override
public void onUpOrCancelMotionEvent() {
}
}
ObservableScrollView的实现:
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();
}
}
下载:
http://download.csdn.net/detail/xyz_lmn/7064257
/**
* @author 张兴业
* http://blog.csdn.net/xyz_lmn
* 我的新浪微博:
@张兴业TBOW