一个iOS风格的边缘滚动效果:overscroll
jopen
9年前
一个iOS风格的边缘滚动效果,支持几乎所有滚动视图。
使用说明:
核心效果类是安卓view的松耦合的--decorators ,因此它和和具体的View是解耦了的。这让开发者在实现效果的同时尽可能像黑盒那样不受干扰。
Gradle 依赖
添加如下到module的build.gradle 文件:
dependencies { // ... compile 'me.everything:overscroll-decor-android:1.0.0' }
RecyclerView
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); // Horizontal OverScrollDecoratorHelper.setUpOverScroll(recyclerView, OverScrollDecoratorHelper.ORIENTATION_HORIZONTAL); // Vertical OverScrollDecoratorHelper.setUpOverScroll(recyclerView, OverScrollDecoratorHelper.ORIENTATION_VERTICAL);
ListView
ListView listView = (ListView) findViewById(R.id.list_view); OverScrollDecoratorHelper.setUpOverScroll(listView);
GridView
GridView gridView = (GridView) findViewById(R.id.grid_view); OverScrollDecoratorHelper.setUpOverScroll(gridView);
ScrollView, HorizontalScrollView
ScrollView scrollView = (ScrollView) findViewById(R.id.scroll_view); OverScrollDecoratorHelper.setUpOverScroll(scrollView); HorizontalScrollView horizontalScrollView = (HorizontalScrollView) findViewById(R.id.horizontal_scroll_view); OverScrollDecoratorHelper.setUpOverScroll(horizontalScrollView);
任意view (Always Over-Scroll Ready)
View view = fragmentView.findViewById(R.id.demo_view); // Horizontal OverScrollDecoratorHelper.setUpStaticOverScroll(view, OverScrollDecoratorHelper.ORIENTATION_HORIZONTAL); // Vertical OverScrollDecoratorHelper.setUpStaticOverScroll(view, OverScrollDecoratorHelper.ORIENTATION_VERTICAL);
高级用法
// Horizontal RecyclerView RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); new HorizontalOverScrollBounceEffectDecorator(new RecyclerViewOverScrollDecorAdapter(recyclerView)); // ListView (vertical) ListView listView = (ListView) findViewById(R.id.list_view); new VerticalOverScrollBounceEffectDecorator(new AbsListViewOverScrollDecorAdapter(listView)); // GridView (vertical) GridView gridView = (GridView) findViewById(R.id.grid_view); new VerticalOverScrollBounceEffectDecorator(new AbsListViewOverScrollDecorAdapter(gridView)); // A simple TextView - horizontal View textView = findViewById(R.id.title); new HorizontalOverScrollBounceEffectDecorator(new StaticOverScrollDecorAdapter(view));
自定义 View
public class CustomView extends View { // ... } final CustomView view = (CustomView) findViewById(R.id.custom_view); new VerticalOverScrollBounceEffectDecorator(new IOverScrollDecoratorAdapter() { @Override public View getView() { return view; } @Override public boolean isInAbsoluteStart() { // canScrollUp() is an example of a method you must implement return !view.canScrollUp(); } @Override public boolean isInAbsoluteEnd() { // canScrollDown() is an example of a method you must implement return !view.canScrollDown(); } });
效果与行为配置
/// Make over-scroll applied over a list-view feel more 'stiff' new VerticalOverScrollBounceEffectDecorator(new AbsListViewOverScrollDecorAdapter(view), 5f, // Default is 3 VerticalOverScrollBounceEffectDecorator.DEFAULT_TOUCH_DRAG_MOVE_RATIO_BCK, VerticalOverScrollBounceEffectDecorator.DEFAULT_DECELERATE_FACTOR); // Make over-scroll applied over a list-view bounce-back more softly new VerticalOverScrollBounceEffectDecorator(new AbsListViewOverScrollDecorAdapter(view), VerticalOverScrollBounceEffectDecorator.DEFAULT_TOUCH_DRAG_MOVE_RATIO_FWD, VerticalOverScrollBounceEffectDecorator.DEFAULT_TOUCH_DRAG_MOVE_RATIO_BCK, -1f // Default is -2 );