Android仿IOS回彈效果 支持任何控件
本文實(shí)例為大家分享了Android仿IOS回彈效果的具體代碼,供大家參考,具體內(nèi)容如下
效果圖:
導(dǎo)入依賴(lài):
dependencies { // ... compile 'me.everything:overscroll-decor-android:1.0.4' }
RecyclerView
支持線性布局和網(wǎng)格布局管理器(即所有原生Android布局)。可以輕松適應(yīng)支持自定義布局管理器。
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);
高級(jí)用法
// 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 進(jìn)行拖動(dòng)
從版本1.0.1起,效果可以與RecyclerView內(nèi)置的滑動(dòng)機(jī)制(基于ItemTouchHelper)平滑運(yùn)行。但是,還需要一些很少顯式的配置工作:
// 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));
滾動(dòng)狀態(tài)改變回調(diào)
// 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原本范圍時(shí)回調(diào)
當(dāng)前拖拽的強(qiáng)度(偏移量)
// 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(); } });
拖拽強(qiáng)度和回彈效果配置
/// 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 );
禁用回彈效果和開(kāi)啟回彈效果
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回彈效果
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android ScrollView的頂部下拉和底部上拉回彈效果
- Android RecyclerView上拉加載更多功能回彈實(shí)現(xiàn)代碼
- android仿QQ個(gè)人主頁(yè)下拉回彈效果
- Android界面上拉下拉的回彈效果實(shí)例代碼
- Android ReboundScrollView仿IOS拖拽回彈效果
- Android ScrollView實(shí)現(xiàn)橫向和豎向拖動(dòng)回彈效果
- Android自定義ScrollView實(shí)現(xiàn)放大回彈效果
- Android編程ViewPager回彈效果實(shí)例分析
- Android自定義控件仿ios下拉回彈效果
- Android基于reclyview實(shí)現(xiàn)列表回彈動(dòng)畫(huà)效果
相關(guān)文章
Android使用Notification實(shí)現(xiàn)通知功能
這篇文章主要為大家詳細(xì)介紹了Android使用Notification實(shí)現(xiàn)通知功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11Android畫(huà)圖并保存圖片的具體實(shí)現(xiàn)代碼
這篇文章介紹了在Android中畫(huà)圖并保存圖片的實(shí)例,以下是具體的實(shí)現(xiàn)方法,有需要的朋友可以參考一下2013-07-07開(kāi)源自研內(nèi)存分析利器Android?Bitmap?Monitor圖片定位詳解
這篇文章主要為大家介紹了Android?Bitmap?Monitor開(kāi)源自研內(nèi)存分析利器,助你定位不合理的圖片使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Android 自定義View實(shí)現(xiàn)計(jì)時(shí)文字詳解
這篇文章主要為大家介紹了Android 自定義View實(shí)現(xiàn)計(jì)時(shí)文字詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04Android自定義gridView仿頭條頻道拖動(dòng)管理功能
這篇文章主要介紹了Android自定義gridView仿頭條頻道拖動(dòng)管理功能,本文通過(guò)實(shí)例代碼效果圖展示給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12Android開(kāi)發(fā)之瀑布流控件的實(shí)現(xiàn)與使用方法示例
這篇文章主要介紹了Android開(kāi)發(fā)之瀑布流控件的實(shí)現(xiàn)與使用方法,結(jié)合實(shí)例形式分析了Android瀑布流控件的定義與使用方法,需要的朋友可以參考下2017-10-10Arduino 數(shù)據(jù)類(lèi)型轉(zhuǎn)換(單機(jī)片)詳細(xì)介紹
這篇文章主要介紹了Arduino 數(shù)據(jù)類(lèi)型轉(zhuǎn)換(單機(jī)片)詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2016-11-11Android簡(jiǎn)單實(shí)用的可拖拽GridView組件分享
在我們?nèi)粘i_(kāi)發(fā)中,使用?GridView?這種網(wǎng)格視圖的場(chǎng)合還是不少的,本篇我們來(lái)介紹一個(gè)支持拖拽的?GridView?組件,可以輕松搞定網(wǎng)格視圖的拖拽排序,需要的可以參考一下2023-06-06