ScrollTricks介绍

来源:互联网 发布:spss mac 破解 编辑:程序博客网 时间:2024/06/06 03:06

http://blog.csdn.net/xyz_lmn/article/details/20557925

ScrollTricks是一个开源控件,实现了两个简单功能:

1、Quick Return:向上滑动时,View也向上滑动并且消失,当向下滑动时,View马上出现。例如Google Now的搜索功能。

2、Sticky:类似的同步滚动,特定的View最多滑动到顶部并保持固定不动。例如大众点评或美团的“立即购买”功能。


[html] view plaincopyprint?
  1. <com.example.android.scrolltricks.ObservableScrollView  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/scroll_view"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent">  
  6.   
  7.     <FrameLayout  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="match_parent">  
  10.   
  11.         <LinearLayout android:layout_width="match_parent"  
  12.             android:layout_height="wrap_content"  
  13.             android:orientation="vertical">  
  14.   
  15.             <View style="@style/Item.Top" />  
  16.   
  17.             <View android:id="@+id/placeholder"  
  18.                 android:layout_width="match_parent"  
  19.                 android:layout_height="@dimen/sticky_height" />  
  20.   
  21.             <View style="@style/Item.Bottom" />  
  22.             <View style="@style/Item.Bottom.Alt" />  
  23.             <View style="@style/Item.Bottom" />  
  24.             <View style="@style/Item.Bottom.Alt" />  
  25.             <View style="@style/Item.Bottom" />  
  26.             <View style="@style/Item.Bottom.Alt" />  
  27.   
  28.         </LinearLayout>  
  29.   
  30.         <TextView android:id="@+id/sticky" style="@style/Item.Sticky" />  
  31.   
  32.     </FrameLayout>  
  33.   
  34. </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的实现:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. public class StickyFragment extends Fragment implements ObservableScrollView.Callbacks {  
  2.     private TextView mStickyView;  
  3.     private View mPlaceholderView;  
  4.     private ObservableScrollView mObservableScrollView;  
  5.   
  6.     public StickyFragment() {  
  7.     }  
  8.   
  9.     @Override  
  10.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  11.             Bundle savedInstanceState) {  
  12.         ViewGroup rootView = (ViewGroup) inflater  
  13.                 .inflate(R.layout.fragment_content, container, false);  
  14.   
  15.         mObservableScrollView = (ObservableScrollView) rootView.findViewById(R.id.scroll_view);  
  16.         mObservableScrollView.setCallbacks(this);  
  17.   
  18.         mStickyView = (TextView) rootView.findViewById(R.id.sticky);  
  19.         mStickyView.setText(R.string.sticky_item);  
  20.         mPlaceholderView = rootView.findViewById(R.id.placeholder);  
  21.   
  22.         mObservableScrollView.getViewTreeObserver().addOnGlobalLayoutListener(  
  23.                 new ViewTreeObserver.OnGlobalLayoutListener() {  
  24.                     @Override  
  25.                     public void onGlobalLayout() {  
  26.                         onScrollChanged(mObservableScrollView.getScrollY());  
  27.                     }  
  28.                 });  
  29.   
  30.         return rootView;  
  31.     }  
  32.   
  33.     @Override  
  34.     public void onScrollChanged(int scrollY) {  
  35.           
  36.         Log.d("onScroll""Y:"+scrollY+"|"+mPlaceholderView.getTop());  
  37.         mStickyView.setTranslationY(Math.max(mPlaceholderView.getTop(), scrollY));  
  38.     }  
  39.   
  40.     @Override  
  41.     public void onDownMotionEvent() {  
  42.     }  
  43.   
  44.     @Override  
  45.     public void onUpOrCancelMotionEvent() {  
  46.     }  
  47. }  


ObservableScrollView的实现:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. public class ObservableScrollView extends ScrollView {  
  2.     private Callbacks mCallbacks;  
  3.   
  4.     public ObservableScrollView(Context context, AttributeSet attrs) {  
  5.         super(context, attrs);  
  6.     }  
  7.   
  8.     @Override  
  9.     protected void onScrollChanged(int l, int t, int oldl, int oldt) {  
  10.         super.onScrollChanged(l, t, oldl, oldt);  
  11.         if (mCallbacks != null) {  
  12.             mCallbacks.onScrollChanged(t);  
  13.         }  
  14.     }  
  15.   
  16.     @Override  
  17.     public boolean onTouchEvent(MotionEvent ev) {  
  18.         if (mCallbacks != null) {  
  19.             switch (ev.getActionMasked()) {  
  20.                 case MotionEvent.ACTION_DOWN:  
  21.                     mCallbacks.onDownMotionEvent();  
  22.                     break;  
  23.                 case MotionEvent.ACTION_UP:  
  24.                 case MotionEvent.ACTION_CANCEL:  
  25.                     mCallbacks.onUpOrCancelMotionEvent();  
  26.                     break;  
  27.             }  
  28.         }  
  29.         return super.onTouchEvent(ev);  
  30.     }  
  31.   
  32.     @Override  
  33.     public int computeVerticalScrollRange() {  
  34.         return super.computeVerticalScrollRange();  
  35.     }  
  36.   
  37.     public void setCallbacks(Callbacks listener) {  
  38.         mCallbacks = listener;  
  39.     }  
  40.   
  41.     public static interface Callbacks {  
  42.         public void onScrollChanged(int scrollY);  
  43.         public void onDownMotionEvent();  
  44.         public void onUpOrCancelMotionEvent();  
  45.     }  
  46. }  



下载:

http://download.csdn.net/detail/xyz_lmn/7064257


/**
* @author 张兴业
*  http://blog.csdn.net/xyz_lmn
*  我的新浪微博:@张兴业TBOW
*/
0 0
原创粉丝点击