中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android仿IOS回彈效果 支持任何控件

發布時間:2020-10-14 09:54:16 來源:腳本之家 閱讀:184 作者:嘎吱喀吧 欄目:移動開發

本文實例為大家分享了Android仿IOS回彈效果的具體代碼,供大家參考,具體內容如下

效果圖:

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回彈效果

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

林芝县| 富阳市| 元氏县| 全南县| 耒阳市| 嘉定区| 榆中县| 贡嘎县| 卢湾区| 布尔津县| 澄城县| 云霄县| 新竹市| 东港市| 资源县| 同仁县| 吉木乃县| 衡阳市| 荣昌县| 和硕县| 阳曲县| 寻甸| 如皋市| 西青区| 稻城县| 渝中区| 安远县| 新巴尔虎右旗| 高邮市| 姚安县| 安泽县| 汨罗市| 西盟| 微山县| 永寿县| 嘉兴市| 瓮安县| 囊谦县| 梨树县| 来凤县| 恩施市|