【Android进阶】ScrollTricks介绍

原文地址:http://blog.csdn.net/xyz_lmn/article/details/20557925


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/dodod2012/9624352






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值