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

*/

Android UI开发第四十篇——ScrollTricks介绍的更多相关文章

  1. Android UI开发第三十篇——使用Fragment构建灵活的桌面

    http://www.lupaworld.com/article-222973-1.html 当我们设计应用程序时,希望能够尽最大限度的适配各种设备,包括4寸屏.7寸屏. 10寸屏等等,Android ...

  2. Android UI开发第四十二篇——实现实现易信的圆形图像和对话列表的图像显示部分

    显示图像时,很多个性化显示,圆形或圆角.气泡等等,我们这一篇文章探讨一下圆形和气泡的显示,仿照易信中的实现,先看下效果图: 代码: public class RoundImageView extend ...

  3. Android UI开发第四十三篇——使用Property Animation实现墨迹天气3.0引导界面及动画实现

    前面写过<墨迹天气3.0引导界面及动画实现>,里面完美实现了动画效果,那一篇文章使用的View Animation,这一篇文章使用的Property Animation实现.Propert ...

  4. Android UI开发第三十九篇——Tab界面实现汇总及比较

    Tab布局是iOS的经典布局,Android应用中也有大量应用,前面也写过Android中TAb的实现,<Android UI开发第十八篇——ActivityGroup实现tab功能>.这 ...

  5. Android UI开发第三十五篇——AppCompat实现Action Bar

    每一位Android开发者对Action Bar这种设计都不陌生了,毕竟它已经发布了至少两年了.Android团队发布Action Bar设计规范时同时放出了ActionBar的Api来支持这种设计. ...

  6. Android UI开发第三十六篇——使用Volley加载图片列表

    Android开发者可能会使用Universal Image Loader或者Square`s newer Picasso这些第三方的库去处理图片的加载,那么Volley是怎么加载图片列表的呢,这一篇 ...

  7. Android UI开发第三十一篇——Android的Holo Theme

    好长时间没写Android UI方面的文章了,今天就闲扯一下Android的Holo主题.一直做android开发的可能都知道,Android 系统的UI有过两次大的变化,一次是android 3.0 ...

  8. 【转】Android UI开发第三十一篇——Android的Holo Theme

    好长时间没写Android UI方面的文章了,今天就闲扯一下Android的Holo主题.一直做android开发的可能都知道,Android 系统的UI有过两次大的变化,一次是android 3.0 ...

  9. Android UI开发第三十四篇——SlidingPaneLayout

    SlidingPaneLayout也是系统支持的高级控件,是Android团对在2013 google IO大会期间更新的Support库(Version 13)中新加入的重要的功能.它支持左右滑动菜 ...

随机推荐

  1. php闭包函数简析

    闭包函数(closures)也叫匿名函数,使用js的童鞋应该比较熟悉.PHP5.3开始引入了闭包的特性. 声明一个匿名函数是: $func = function() { }; //带结束符 匿名函数因 ...

  2. C语言实现冒泡排序-整数排序

    我一直觉得排序算法挺重要的,但是却没有深入的去理解它: 没有深入理解就无法用代码将它实现: 在腾讯的在线模拟考试中就有一题问到冒泡排序: 我几乎是傻眼了!我知道这样的问题是最基础的: 无论过去怎样现在 ...

  3. paip. uapi 过滤器的java php python 实现aop filter

    paip. uapi 过滤器的java php python 实现aop filter filter 是面向切面编程AOP.. 作者Attilax  艾龙,  EMAIL:1466519819@qq. ...

  4. springmvc中@PathVariable和@RequestParam的区别

    顾名思义, @PathVariable和@RequestParam,分别是从路径里面去获取变量,也就是把路径当做变量,后者是从请求里面获取参数. 我的url; http://localhost:808 ...

  5. WizardDialog 进度条使用记录

    开发RCP的朋友们经常会使用到导航窗口, 先简单介绍一下wizardDialog,基本上他的使用方法是这样的 首先有一个WizardDialog,在dialog里面需要放一个Wizard来控制页面Wi ...

  6. CSS水平垂直居中总结

    行内元素水平居中 把行内元素包裹在块级父元素中,且父元素中的css设置text-align:center; <!DOCTYPE html> <html> <head> ...

  7. 使用 multiprocessing.dummy 执行多线程任务

    # -*- coding: utf-8 -*- # from multiprocessing import Pool 多进程 from multiprocessing.dummy import Poo ...

  8. CAN Timing Sample Point

    typedef struct { //char name[ 16 ]; // Name of the CAN controller hardware //uint32_t ref_clk; // CA ...

  9. Ios开发之sqlite

    Sqlite是ios数据存储的一个重要手段,今天我们就一块来看一下,怎样使用sqlite将数据存储到沙盒中去. 第一步:导入一个框架libsqlite3.0.dylib 选中TARGETS在Gener ...

  10. 服务器断电,Oracle数据库无法启动解决方案

    数据库没有备份的情况下,数据库所在服务器由于意外断电,导致服务器启动之后,Oracle数据库startup报错. 1. 数据库没开归档模式 2. 无备份 解决方案: 1 2 3 4 5 6 7 8 9 ...