【Android进阶】ScrollTricks介绍
2016-02-29 16:04
323 查看
原文地址:/article/1361166.html
ScrollTricks是一个开源控件,实现了两个简单功能:
1、Quick Return:向上滑动时,View也向上滑动并且消失,当向下滑动时,View马上出现。例如Google Now的搜索功能。
2、Sticky:类似的同步滚动,特定的View最多滑动到顶部并保持固定不动。例如大众点评或美团的“立即购买”功能。
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的实现:
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(); } }
相关文章推荐
- Android使用Intent在Activity之间传递对象[Serializable或者Parcelable]
- [Android]使用PagerSlidingTabStrip来实现选项卡切换界面的示例
- Android开发:shape和selector和layer-list的(详细说明)
- Android - 重要知识点
- Android官方Training阅读笔记 ---- Managing the Activity Lifecycle(Recreating an Activity) (四)
- Android学习之异步任务AsyncTask
- 【android开发】手写签名系统的设计与实现
- Calendar和Date-Android Day09
- Android 使用代码进入Recovery模式自动升级指定固件包
- android studio快捷键
- Android组件——使用DrawerLayout仿网易新闻v4.4侧滑菜单
- android 调用系统相册和系统拍照功能的Demo
- 以一个着色游戏展开讲解Android中区域图像填色的方法
- Android APK反编译就这么简单 详解(附图)
- Android studio下如何使用断点调试功能
- android动态换肤系列2——android.view.LayoutInflater.Factory的使用
- Android 广播机制 详解
- Android事件分发机制完全解析,带你从源码的角度彻底理解dispatchTouchEvent,onInterceptTouchEvent
- android 一键锁屏实现
- android Services