Android仿IOS回弹效果 支持任何控件
程序员文章站
2022-06-23 14:56:50
本文实例为大家分享了android仿ios回弹效果的具体代码,供大家参考,具体内容如下
效果图:
导入依赖:
dependencies {
// ....
本文实例为大家分享了android仿ios回弹效果的具体代码,供大家参考,具体内容如下
效果图:
导入依赖:
dependencies { // ... compile 'me.everything:overscroll-decor-android:1.0.4' }
recyclerview
支持线性布局和网格布局管理器(即所有原生android布局)。可以轻松适应支持自定义布局管理器。
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);
viewpager
viewpager viewpager = (viewpager) findviewbyid(r.id.view_pager); overscrolldecoratorhelper.setupoverscroll(viewpager);
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);
any view - text, image…
view view = 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)); // viewpager viewpager viewpager = (viewpager) findviewbyid(r.id.view_pager); new horizontaloverscrollbounceeffectdecorator(new viewpageroverscrolldecoradapter(viewpager)); // a simple textview - horizontal view textview = findviewbyid(r.id.title); new horizontaloverscrollbounceeffectdecorator(new staticoverscrolldecoradapter(view));
recyclerview 使用 itemtouchhelper 进行拖动
从版本1.0.1起,效果可以与recyclerview内置的滑动机制(基于itemtouchhelper)平滑运行。但是,还需要一些很少显式的配置工作:
// normally you would attach an itemtouchhelper & a callback to a recyclerview, this way: recyclerview recyclerview = (recyclerview) findviewbyid(r.id.recycler_view); itemtouchhelper.callback mycallback = new itemtouchhelper.callback() { ... }; itemtouchhelper myhelper = new itemtouchhelper(mycallback); myhelper.attachtorecyclerview(recyclerview); // instead of attaching the helper yourself, simply use the dedicated adapter new verticaloverscrollbounceeffectdecorator(new recyclerviewoverscrolldecoradapter(recyclerview, mycallback));
滚动状态改变回调
// note: over-scroll is set-up using the helper method. ioverscrolldecor decor = overscrolldecoratorhelper.setupoverscroll(recyclerview, overscrolldecoratorhelper.orientation_horizontal); decor.setoverscrollstatelistener(new ioverscrollstatelistener() { @override public void onoverscrollstatechange(ioverscrolldecor decor, int oldstate, int newstate) { switch (newstate) { case state_idle: // no over-scroll is in effect. break; case state_drag_start_side: // dragging started at the left-end. break; case state_drag_end_side: // dragging started at the right-end. break; case state_bounce_back: if (oldstate == state_drag_start_side) { // dragging stopped -- view is starting to bounce back from the *left-end* onto natural position. } else { // i.e. (oldstate == state_drag_end_side) // view is starting to bounce back from the *right-end*. } break; } } }
拖拽出view原本范围时回调
当前拖拽的强度(偏移量)
// note: over-scroll is set-up by explicity instantiating a decorator rather than using the helper; the two methods can be used interchangeably for registering listeners. verticaloverscrollbounceeffectdecorator decor = new verticaloverscrollbounceeffectdecorator(new recyclerviewoverscrolldecoradapter(recyclerview, itemtouchhelpercallback)); decor.setoverscrollupdatelistener(new ioverscrollupdatelistener() { @override public void onoverscrollupdate(ioverscrolldecor decor, int state, float offset) { final view view = decor.getview(); if (offset > 0) { // 'view' is currently being over-scrolled from the top. } else if (offset < 0) { // 'view' is currently being over-scrolled from the bottom. } else { // no over-scroll is in-effect. // this is synonymous with having (state == state_idle). } } });
自定义控件
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 );
禁用回弹效果和开启回弹效果
ioverscrolldecor decor = overscrolldecoratorhelper.setupoverscroll(view); // detach. you are strongly encouraged to only call this when overscroll isn't // in-effect: either add getcurrentstate()==state_idle as a precondition, // or use a state-change listener. decor.detach(); // attach. decor.attach();
源码地址:android仿ios回弹效果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。